1
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?

2
  • 1
    Usually one would - when used among multiple classes - store such constants in a XyzConstants class or even enum keeping only static final fields. Commented May 17, 2015 at 18:59
  • 1
    Generally, criticism of "statics" is about static state. Genuine static methods, like mathematical operations, or constants, are fine (though many constants would be better replaced with enums). Commented May 17, 2015 at 19:42

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

HI @Serkham, as I've said in my question, these values will not change, they are used purely to aid readability and will therefore be the same values for all instances of this class that are created. So I don't quite understand why I can't use use 'final static' - indeed I have tried it and it does work. My question really is whether this is good practice. Thanks
if you think all of other 99 objects use same variabls so you can use nonfinal static variable but at this time you don't need create and pass instance variables because of you can use these variables by MyObject.x everywhere.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
1

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...
}

2 Comments

Thanks @Mureinik and you can't see any problems with putting these static values into their own class? (As I mention in my question)?
If these variables don't make sense anywhere outside the context of this class, it makes no sense to put them into a "statics class". If they should be shared between several other classes it may be acceptable, although, personally, I dislike this style.
1

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?

3 Comments

Hi @ChristopherOezbek, yeah sorry, my question had a mistake, I've updated it so it's clearer as to what I'm actually doing. My project is drawing a object which is basically made up from a bunch of openGL quads (which are batched as one java object). Thanks for your suggestions!
Are you really expecting an update on the answer or more answers if you do not up-vote the answers you got?
I updated my question @ChristopherOezbek, because it seemed a little confusing to me when I read through it, so I tried to make it clearer. I'm not expecting anything more that to make it easier for people who read it in the future to better understand it. I don't understand your comment as I wasn't expecting anything at all. I will upvote and select an answer when I'm ready to. I only asked this 2 hours ago.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.