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

  • SEARCH
  • Home
  • 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 7903913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:52:06+00:00 2026-06-03T09:52:06+00:00

Ok my problem isnt really a serious one, im just trying to find a

  • 0

Ok my problem isnt really a serious one, im just trying to find a clever way of access/modification of class member variables. Here is the code:

public class Storage{
 private int cookies= 0; 
 private int rolls= 0; 
 private int candies= 0; 
 private int lolipops= 0; 
 private int iceCreams= 0; 

 public void addCookies(int howMuch){  //this is the dirty way of creating method for
    this.cookies = cookies+howMuch;     //every member variable    
 }

 public void addValue(String stat, int howMuch){ //i would like to do it only
                                       //by passing the name
                                      //of variable and then cast it as integer
                                      //so that it would relate to my class members

    int value = this.(Integer.parseInt(stat));   //  <- YES i know its ridiculous
                                      //im just trying to explain what is my aim       
    value = value + howMuch;

    this.(Integer.parseInt(stat)) = value;
 }
}

Generally i would like to access a field by passing its name to a method, read value of that member, add to it some value, and then store it. Yes i know that it easily can be done with separate methods, or even with one by using some arraylist and comparisons of member names with parameter passed to method. But i would like to do it “fast” without redundant code writing.

Now i have like 5 members, but what about 15000? My aim is to simplify the whole processing and code writing. So generally is it possible to do such redundant code writing bypass? Since i know that i will always pass appropriate name to method… Unless the rule of thumb is to create method for each variable?

  • 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-06-03T09:52:08+00:00Added an answer on June 3, 2026 at 9:52 am

    I guess that by using reflection you can iterate through the fields/methods of your object and do your computation.

    For one specific field:

        Field member = myObject.getClass().getField(fieldName);
        // If you know the class: Field member = MyClass.class.getField(fieldName);
        System.out.println(member.getInt(myObject)); // Get the value
                member.setInt(myObject, 4); // Set the value
    

    If you want to something for all the public members:

        for(Field member: myObject.getClass().getFields())
            // Or you can do: for(Field member: myClass.class.getFields())
        {
            member.getInt(myObject)); // Get the value
            member.setInt(myObject, 4); // Set the value
        }
    

    Basically, what you do is that you find the Field object that represents the members of you object, then you can manipulate it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having a serious problem with my C code, I just don't seem to be
This question isn't really a problem looking for a solution, it's more just a
Yes I know, this title isn't really helpfull but this is the exact problem.
I know my question isn't about programming problem, I just need a suggestion to
If the title isn't clear enough, here's a procedural way of approaching the problem:
I have this really strange problem where my entity framework query isn't enumerating correctly.
I have a problem where I'm trying to use a function from another module
Is there any problem in initializing Sets in the constructor of a class persisted
Im trying to debug where the problem is in this dynamic input generator tool
It isn't really causing a problem but it is annoying me... Does anyone know

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.