Zippy
Zippy

Reputation: 3887

Using variables / Static variables to 'name' values to aid readability

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?

Upvotes: 1

Views: 101

Answers (3)

Christopher Oezbek
Christopher Oezbek

Reputation: 26473

There is some practice in Java to use final static for the purpose of naming integer values (see for instance http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingConstants.html), but not specifically for the purpose you describe:

Using an array to store semantically different data should be considered in most cases to be a code smell. In your example you use Y = 1000 but get it confused in your own explanation where the equivalent is suddenly 1.

I think in your example, you either meant to describe a 2D array or are you considering that X might never exceed 999?

My suggestions what you should do:

  • Use an enum to avoid having a bunch of Xs and Ys flying around. And hey enums use static finals for the enum values too!
  • If you really must use static final ints, be smart to name them X-DIM and Y-DIM or something not to confuse readers. X and Y do not indicate anything to the reader.
  • If you can map a problem to using static one-to-one mapping, have you considered using a class with members rather than an array?
  • If this is really rather about low level programming and preparing data for serialization, how about using a tailored solution such as Google Protobufs?

Upvotes: 1

Sarkhan
Sarkhan

Reputation: 1289

Imagine you have 100 of MyObject objects and all members are final and static.This means that all of these 100 objects have same nonchangeable static variables because of final.so you can't change these members in other 99 objects.So you can't use static and final together in your situtation.

think about without final only static members.This means that you don't need create instance objects of MyObject class because you can use these variables everywhere(places where you wanna pass these variables there) by Class name.

if you want every instance object has spesific variables that you can change these after you should use instance objects and non static and non final variables.

i think in your situtation using static variables is good because all of 100 objects use same variables and same values.So you will not take place additional memory for all of objects.Actualy you don't need 100 objects.You need just one class and its static non final variables

Upvotes: 1

Mureinik
Mureinik

Reputation: 312056

static variables are considered "bad" as they complicate the state of the object. One instance can be affected by another instance, unintentionally, which may lead to hard to diagnose bugs. For final primitives (read: constants), this is not an issues, and there is really not problem in having them:

public class myObject extends someOtherObject(){

    private static final int X = 0;
    private static final int Y = 1000;

    // rest of the class...
}

Upvotes: 1

Related Questions