public class myObject extends someOtherObject(){
final int MAINBOX_X = 0;
final int MAINBOX_Y = 1;
final int BUTTON_X = 2;
final int BUTTON_Y = 3;
final int TEXT_X = 4;
final int TEXT_Y = 5;
public myObject(int x, int y){
super();
//'coordinates' is an array of pairs, x and y for example
coordinates[MAINBOX_X] = x;
coordinates[MAINBOX_Y] = y;
coordinates[BUTTON_X] = coordinates[MAINBOX_X]+10;
coordinates[BUTTON_Y] = coordinates[MAINBOX_Y]+10;
coordinates[TEXT_X] = coordinates[MAINBOX_X]+10;
coordinates[TEXT_Y] = coordinates[MAINBOX_Y]+50;
//This aids readability because the alternative would be:
//coordinates[0] = x; //Where 0 is the index representing the object's x coordinate
//coordinates[1] = y; //Where 1 is the index representing the object's y coordinate
//coordinates[2] = coordinates[0]+10;
//coordinates[3] = coordinates[1]+10;
//etc....
//This could easily get very confusing without using proper/recognisable names!
}
}
Now if I create a lot of theses objects. Let's say 400 of them. (Note, this is only a simple example, there are a lot more than just X and Y values in the actual project).
I've read all the 'statics are bad' kind of posts and what I'm trying to understand is when I create all of these objects, there will be multiple copies of these final int's in memory - which seems unnecessary to me as the values are always the same.
However, if I declare them as statics/class variables, then all of the objects, will share one copy of the variables and therefore, there will only ever be a single copy created - more memory efficient?
Separate class for statics
An alternative is to create a 'values' class and house the statics - just to keep things tidy. (Although to be honest, I could simply pass a reference of the original class/object to the class which needs to access these values).
Is using static/class variables for this purpose acceptable practice? If not, what is a better alternative?
XyzConstantsclass or even enum keeping onlystatic finalfields.