2

I have a dilemma because I don't know what is better solution. I have a static variable.

I wonder what is the best practice of declaring these variables. Let's suppose that I have such a variable in myStatic class.

public class myStatic(){

    public static int integer = 0;

    /* get value */
    public int getInteger() {
        return integer;
    }

    /* set value */
    public void setInteger(int nInteger) {
        integer = nInteger;
    }

}

Now I must increment this variables or decrements. How to do it correctly?

1)

myStatic.integer++;

2)

myStatic mystatic = new myStatic();
int integer = mystatic.getInteger();
int nInteger = integer+1;

mystatic.setInteger(iInteger);

Is better using solution 1 or 2?

5
  • Just my two cents as I have never seen in hard concrete which is preferred, but generally speaking in my personal code I directly access static variables instead of using getters and setters. When reading my code it tends to make it easier for me to read as I know immediately if I am working with a static variable or not based on how I am accessing it. Commented Feb 4, 2016 at 16:42
  • 2
    They are both essentially doing the same thing here. Though if you were going to do it through option 2, there really is no need for an integer to be static. Commented Feb 4, 2016 at 16:42
  • 3
    It is generally good to avoid static altogether; the bigger your project gets the more you hate static variables. Commented Feb 4, 2016 at 16:43
  • This is not a good case to use static variable. Singleton seems to be more appropriate for your case. Commented Feb 4, 2016 at 17:09
  • Can you tell me why? Commented Feb 4, 2016 at 18:07

4 Answers 4

1

I would go with number 1, 100%, maybe just because I'm lazy, but kind of also because of:

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

This principle has been a key, and a huge success in my years of software engineering. A common problem among software engineers and developers today is that they tend to over complicate problems.

Principle of extreme programming (XP) that states a programmer should not add functionality until deemed necessary.

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

3 Comments

I see the point that these links make and I agree with it to some extent, however I have found in practice that if you have time to add a feature, even if it isnt needed currently, it could save a lot of time in the future from having to come back and refactor it and add the functionality that you want. It often pays to be forward thinking in your approach to a new piece of code.
@Devsil Yeap, I would say that it really depends on how do you feel with a feature and how do you visualize it, if it is going to be a core feature of your system you would rather have it well defined before you actually use it.
@Devsil And... accessing a static variable should not be involved in any core feature of any system, as Andrew mentioned. It might cause memory leaks and it is going to be hard to test.
0

If that variable needs to be accessed everywhere and at any time, you should go with option 1. It will act as an Environment variable even tho its not reallyyyy the same thing.

more info on env vars: https://en.wikipedia.org/wiki/Environment_variable

Comments

0

Static variables need not be accessed through an object. Infact it is a waste of code. Consider this :

public class MyStatic {
     public static int i = 0;
}

You can directly access the static variable like this :

private MyStatic myStatic = null;
myStatic.i++;

This is because, the JVM doesn't even care about the object for a static property.

Comments

0

since static vars are class variables, they can be manipulated by any object, unless you declare a static variable as private, you had to access to it via public static methods. Then, your first approach is correct, in the second the method getInteger() does not work. http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

I recomend you to read about the singleton pattern design.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.