Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3232714
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:11:35+00:00 2026-05-17T17:11:35+00:00

Looking at some code cleanup and I was wondering the best way to deal

  • 0

Looking at some code cleanup and I was wondering the best way to deal with this:

Have a class with some private variables like:

myBool1, myBool2, myBool3
myInt1, myInt2, myInt3
myString1, myString2, myString3

What’s the best way to do a getter function that is generic to the return value? So if I do a call to the getter with something like:

myNewBool=<blah>.get("myBool1")
myNewString=<blah>.get("myString2")
myNewInt=<blah>.get("myInt3") 

Anyone have any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:11:35+00:00Added an answer on May 17, 2026 at 5:11 pm

    You can’t really have a generic getter if you don’t know what you want to get, for example :

    boolean myNewBool= get("myString1");
    

    If get returns something, but you don’t really know if this something is compatible with a boolean, and terrible things could happen.

    You could try this:

    public <T> get(String element){
        return (T) elementToGet;
    }
    

    But you would have to specify the return type when you call the get method.

    String element = myObject.<String>get("element");
    

    Here are the bad sides :

    • You can’t work directly with primitives
    • You can have a lot of ClassCastException
    • If you misspell an attribute name you won’t see it until you run it
    • You don’t expose a nice public API, people would have to know evert possible attribute to use it, and as said above, a misspelled attribute (or an inexistant one) wouldn’t be seen until runtime.
    • You have to know the return time and type it each time you use your method
    • You would have to type a really long (and smelly) code in your get method either to use each possible attribute (if you still want have some private and not accessible) or worse, use reflection to find the right attribute.

    So definitively not a good idea.

    What you can do instead is using the good old getters//setters and if there is a lot of them, generate them with your IDE.

    Another way would be to use the project lombok.


    Resources :

    • Project Lombok

    On the same topic :

    • Create automatically only getters in Eclipse
    • Eclipse Generate All getters setters in package
    • Java Getters and Setters
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking at some code and was wondering how this works. In one class,
So I'm looking through some code, and I see this: class whatever { public:
I was looking over some code that another developer wrote and found this: Private
Looking through some code I came across the following code trTuDocPackTypdBd.update(TrTuDocPackTypeDto.class.cast(packDto)); and I'd like
I'm looking through some code for learning purposes. I'm working through this portion of
I was looking at some code and they had this line: #define WINVER 0x0501
Im looking through some code (unfortunatly the author isnt around anymore) and im wondering
While looking over some code in Think Complexity , I noticed their Graph class
Looking at some code written by another developer, I came across this: for($i=1; $i<=30;
When looking through some code I took over, I came across this line: my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.