4

According to Java, static variable are accessible by Class name but they are also accessible by class object even though Java don't suggest it, and it gives the same answer.

I know there will be only one copy of the variable and its value will be same for all objects and other things. Why does Java suggest to use class name instead of class object?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
maddygoround
  • 2,145
  • 2
  • 20
  • 32
  • 6
    It makes the code more self-documenting. It's a variable of the class, not of the instance. – BalusC Sep 20 '12 at 04:32
  • @BalusC so if i use object instead of class name would it be wrong way to write programs.other than making code self-documenting is there any performance reasons. – maddygoround Sep 20 '12 at 04:35
  • 1
    I think you've got the right instinct to want to learn the typical paradigms used by Java programmers, but I don't think there is any reason to worry about performance on this kind of micro-optimization level. Anyway, no, there shouldn't be a performance hit. – Tim Bender Sep 20 '12 at 04:43
  • @BalusC I've read the documentation on this before, but your simple explanation finally got it through my thick skull! Thanks for that, very useful! – RossC Sep 20 '12 at 07:34

3 Answers3

6

Because it can be confusing! There is no dynamic dispatching on static members.

Take a look at this confusing code: (might be syntax errors; my Java is rusty)

public abstract class Singer {
    public static void sing() {
        System.out.println("Singing");
    }
}

public class Soprano extends Singer {
    public static void sing() {
        System.out.println("Singing in the range of C4-A5");
    }
}

public class MyDriver {

    public static void main(String[] argv) {
        Singer  mySoprano1 = new Soprano();
        Soprano mySoprano2 = new Soprano();
        mySoprano1.sing();
        mySoprano2.sing();
    }

}

Looking at MyDriver it's confusing because it seems like the sing method is polymorphic so the output should be...

Singing in the range of C4-A5
Singing in the range of C4-A5

... because both soprano1 and soprano2 are instances of Soprano - not Singer.

But alas, the output is actually:

Singing
Singing in the range of C4-A5

Why? Because there is no dynamic dispatch on static members, so the declared type of mySoprano1 determines which sing method is invoked... and the declared type of soprano1 is Singer, not Soprano.

For more, check out Puzzle 48 "All I get is static" in the book Java Puzzlers.

Richard JP Le Guen
  • 28,364
  • 7
  • 89
  • 119
  • @Makoto, I think the point of the answer is to point out that it is ambiguous which `sing` method will be invoked since s1 is referred to as a `Singer` and s2 is referred to as a `Soprano` despite both being instances of `Soprano`. – Tim Bender Sep 20 '12 at 04:40
  • It's not ambiguous per se - the compiler knows in the end - but for the developer it is misleading. It makes it look like `Soprano.sing()` will be invoked, when `Singer.sing()` will be invoked on account of the declared type of `mySoprano1`. – Richard JP Le Guen Sep 20 '12 at 04:43
  • well it simply calls the sing() function..first time for a base class and then for the child class.so what if i want to call the sing() of base class using chlid class name can i do like Soprano.sing() would it call the function sing() of super class. – maddygoround Sep 20 '12 at 04:43
  • I suppose I'm not a fan of the example. – Makoto Sep 20 '12 at 04:46
  • @RathoreSunny - I'm not sure I follow what you want. ? – Richard JP Le Guen Sep 20 '12 at 04:49
  • to call sing() of singer class using Soprano class.it can be done by objects but can it be done by Class name – maddygoround Sep 20 '12 at 04:52
  • Only using the explicit class name: `Singer.sing()`. I don't think you can use the [`super` keyword](http://docs.oracle.com/javase/tutorial/java/IandI/super.html) from a static context. – Richard JP Le Guen Sep 20 '12 at 04:55
  • See also [calling a super method from a static method](http://stackoverflow.com/questions/5059051/calling-a-super-method-from-a-static-method) – Richard JP Le Guen Sep 20 '12 at 04:56
  • that's my point objects can do that i think.But using class name its not possible. – maddygoround Sep 20 '12 at 04:58
  • i saw the link but as one can see still they are accessing the static function using class objects not the class name – maddygoround Sep 20 '12 at 04:59
4

It is more self-documenting if you write MyClass.staticVariable than myObject.staticVariable. It tells the person looking at the code that staticVariable is a property of MyClass, as opposed to myObject which is a particular instance of the class.

tskuzzy
  • 35,812
  • 14
  • 73
  • 140
  • Besides, the compiler generates exactly the same code, since at compile time it is already clear what static variable you are referring to. – jan.vdbergh Sep 20 '12 at 04:40
0

One point I can think is if you use Class Reference instead of Object, JVM does not need to create a new Object at all to access that static variable. This is a good programming practice for performance.

Binu
  • 120
  • 6
  • 3
    You don't need an onject, though, just a reference. `Foo foo = null; foo.myStatic;` will work just fine (and not throw a NPE). – yshavit Sep 20 '12 at 06:38