I have a class that stores a large number of int
member variables, each defined as follows:
public final static int int1 = int1value;
public final static int int2 = int2value;
...
public final static int int106 = int106value;
There is a second class that needs to do a loop based on the number of int
s in the first class. How would I go about adding a function to the first class to count these member variables?
How are you storing it? In anint[]
array or aList<Integer>
list I assume? That would be the most logical choice. You can then just get the number byintArray.length
orintList.size()
.Check the Sun tutorials to learn more about Arrays or Collections.
Those are all declared as
public static final int
fields? Why don't you just add the count as anotherpublic static final int
field? You already know the count beforehand! Or it must be a 3rd party class which you can't change in any way. It should have been more clarified in the original question.Anyway, you can also consider to grab reflection. Here's an SSCCE:
How lame!
Unfortunately there is no neat way to sum the values of a large number of statics. You could do it using reflection (see @BalusC's answer), or simply by writing a method evaluates hard-wired expressions like:
Your real problem is in your program design. The use of statics is dubious, the use of 160 named variables (instead of an array or list) is bad, and even putting the method(s) in a separate class is dubious.
Fix the design problems and you don't have this one.
If the values (integers) are stored in a collection (myCollection):
If it's an array:
Or.. if it's completely different, you could add a counter to the first class and do it like this:
So how does your first class store these ints ? In a List ? Why not use the collection to return the number of ints stored.
e.g
Note that from a design POV, I'd rather that the first class loops round the ints it's stored and calls out to a client class for each one (I assume the second class has to do something with each int), but that's being a little pedantic.
Based on your edit, it seems like you always have 106 numbers, all of which are initialized... is that correct? If so, you don't need to count, just return 106.
EDIT: @PuppyKevin, based on your comment, what you can do is make another constant variable to keep track of the total number of ints in class one, like
and just refer to that in class two, like
That way you only have to remember to update one place, and it's in the file you're already editing, which will reduce mistakes.
Also, do you have a specific reason for avoiding arrays or collections? If not, as you might expect from the other answers, I encourage you to use one of those. It's better style for a number of reasons, and would eliminate the problem you're asking about in this post.
Using a collection or an array is the best approach, but assuming you won't take that advise you can use reflections.
Or assuming you can count the fields yourself. i.e. you know how many fields you put in your class, just return a constant.