4

Recently I was going through a page on javarevisited and found a block of code which asked the readers to determine what would be the output for it...

Though I got the output I am not satisfied with the result (WHICH CAME OUT TO BE "Hello") since I don't know how a static member is accessed from a null reference. What's happening in the background?

public class StaticDEMO {

    private static String GREET = "Hello";

    public static void main(String[] args) {
        StaticDEMO demo = null;
        System.out.println(demo.GREET);
        // TODO code application logic here
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lalit Rao
  • 551
  • 5
  • 22
  • Because it doesn't actually use the reference. – user253751 Jan 07 '15 at 06:00
  • 1
    `StaticDEMO demo;` The compiler knows that `demo` is a class reference, it maybe null but it is of type `StaticDEMO`, which is your class.. this is not an object... .. Since static fields are determined at compile time, this is OK. – Sharp Edge Jan 07 '15 at 06:08

4 Answers4

12

This works because the JVM knows you are trying to access a static member on a specific class. Because you had to declare demo as a specific class (in this case a StaticDEMO), it knows to use that to find GREET.

To be clear, you don't run into this often (I actually had to type this code in to verify it, I can't say I've ever seen this). Mainly, it's good practice to always refer to static fields by their class, not an object instance (which may be null, as we can see!).

Meaning, prefer this:

System.out.println(StaticDEMO.GREET);

EDIT

I found a reference to this in the Java Specification: Chapter 15, Section 11: Field Access Expressions.

Example 15.11.1-2. Receiver Variable Is Irrelevant For static Field Access

The following program demonstrates that a null reference may be used to access a class (static) variable without causing an exception

[example not shown here for brevity]

Community
  • 1
  • 1
Todd
  • 30,472
  • 11
  • 81
  • 89
  • 1
    But looking at the block of code, it is likely that it would throw NULLPOINTEREXCEPTION or NULLREFERENCEEXCEPTION at run time.. My query is how did this work??? We have been taught that static members are class members and can only be accessed by name of the class. So, how, in this case, a static member is accessed from a null reference variable? :/ – Lalit Rao Jan 07 '15 at 06:06
  • 3
    Because it's not using the reference at all, just the type of the reference. Even when you use an object instead of the class to refer to a static field, the JVM knows the type of object (the CLASS) and uses that under the covers. The spec specifically calls this legal (Sec 15.11.1). – Todd Jan 07 '15 at 06:08
4

Any method which is being decorated as static in Java means, that method is a class level memeber. That means, you do not need an object to accss static members. The static method/variable is maintained by the Class itself, and not by any instance of the class. Here in your example, the compiler already knows that the member is static member and it doesn't need any instance to access that static method.

Yadu Krishnan
  • 3,492
  • 5
  • 41
  • 80
  • I totally agree with you @Yadu, i know the static method/variable is maintained by the Class itself, and not by any instance of the Class but my question is how it is accessed from a null reference? – Lalit Rao Jan 07 '15 at 06:10
  • Since the JVm knows that the method you are trying to access is a static one, it ignores the instance. Because, the same method/same result will be returned even if you try to access the static method with different instances of the class. – Yadu Krishnan Jan 07 '15 at 06:12
2

Static members are stored with the Class, not with any specific instance of it. So, it doesn't matter that the instance is null - the member from the Class is still be accessible.

Mike Smith
  • 43
  • 4
0

The JVM simply ignores null, because GREET is a class field and demo is irrelevant reference to refer Class field.

Static method not needed object reference to call it so you can call it even reference to the object is null.

atish shimpi
  • 4,873
  • 2
  • 32
  • 50