0

If I have this sample code:

public class Test{

static String myVariable = "hi";

public Test(){
    System.out.println(myVariable);
    System.out.println(Test.myVariable);
}

public static void main(String[] args) throws SQLException{
        new Test();
    }

}

now both will print "hi" but I want to know what differentiates them and when should I use one over the other or if I should always only use one of them as it is the norm.

I'm sorry for this really basic example but I didn't really bother learning the difference between the two as I got my system to work

makingitwork
  • 149
  • 2
  • 9
  • There is no difference. If you are in the class which defines the variable, you do not need to scope it with the class name. – Mr. Polywhirl Mar 21 '16 at 15:35
  • @Mr.Polywhirl so there is no rule I'm breaking when I use myVariable instead of Test.myVariable? or is it just pure preference? – makingitwork Mar 21 '16 at 15:36
  • Using `Test.myVariable` explicitly states that you are referencing a static variable, while using `myVariable` could reference a scoped variable. Some people will prefer the most explicit version, most will probably not care. – Aaron Mar 21 '16 at 15:39
  • @makingitwork no, there is no "rule". It is also not "pure preference" either - the former is shorter (which is nice), the latter is more explicit (which is safer). Which you should prefer will depend on context. – dimo414 Mar 21 '16 at 15:40

5 Answers5

6

They both refer to the same variable, so in principle they're identical. In practice however the first choice risks collisions with a local variable; for example adding String myVariable = "bye"; before your first println() would output bye followed by hi.

There is certainly no "rule" you break by using one or the other. Use whichever makes your code more readable, but beware of conflicting variable names. Eclipse and other IDEs can be set to warn you when you do so, which is very helpful.

Community
  • 1
  • 1
dimo414
  • 47,227
  • 18
  • 148
  • 244
  • So it is a matter of preference and if you are that well disciplined in naming your local variables not to conflict with the global ones, I guess I'll start practicing using the 2nd option where you specify the class in the variable. I came to ask this question because I read this [link](http://stackoverflow.com/questions/19035916/static-variable-vs-non-static-variable). Thank you – makingitwork Mar 21 '16 at 15:41
  • @makingitwork being well-disciplined is hard, but using IDE tools is easy. With Eclipse's warnings I know immediately when I've made a mistake in my variable naming, and therefore it's never a problem. That said, erring on the side of being more explicit is rarely going to be a problem. – dimo414 Mar 21 '16 at 15:44
2

You'll prefer Test.myVariable when you want to make sure you're calling the static variable from the class or when you want to refer to it from another class (if it's not private and preferably final)

public class Test {
    static String myVariable = "hi";
    public Test(){
        String myVariable = "hello";
        System.out.println(Test.myVariable);  // hi
        System.out.println(myVariable);       // hello

    }
}
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
1

There is no difference if you are in the that class. Both myVariable and Test.myVariable point to the same object.

If you were calling myVariable from another class you'd have to use Test.myVariable syntax to refer to the correct object.

Shmuel
  • 3,916
  • 2
  • 27
  • 45
0

There is no difference. But you should always use Test.myVariable because it directly indicates that it is a static variable to the person who reads your code otherwise it can lead to hard-to-find bugs.

Heisenberg
  • 3,153
  • 3
  • 27
  • 55
  • 4
    "**always**" is a bit strong. – dimo414 Mar 21 '16 at 15:36
  • @dimo414: I agree. No one prefixes a `static final` inside the class which defines it. ;) – Mr. Polywhirl Mar 21 '16 at 15:37
  • That seems excessive. All IDEs I've used for the past 5 years were able to highlight static variables as such, and you can always just check where a variable declaration is located. Not to mention that if your class is so gargantuan that you can't figure out where its variables come from anymore, it's probably not terribly well factored anyway. – Cubic Mar 21 '16 at 15:38
0

For the first one, you are printing myVariable directly. For the second one, you are printing myVariable from Test class. You are referring to the same entity. I would recommend using Test.myVariable to avoid confusion later on as your program gets larger. However, technically it's your choice as to the way you want to present this static variable. Sometimes it may make more sense for you to have a static variable represented as myVariable. Just be consistent. There is a reason why they allow both methods :).

Floam
  • 704
  • 1
  • 8
  • 20