10

The following simple code snippet is working fine and is accessing a static field with a null object.

final class TestNull
{
    public static int field=100;

    public TestNull temp()
    {
        return(null);
    }
}

public class Main
{
    public static void main(String[] args)
    {
        System.out.println(new TestNull().temp().field);
    }
}

In the above code, the statement System.out.println(new TestNull().temp().field); in which the static field field is being associated with a NULL objectnew TestNull().temp(), still it is returning a correct value of it which is 100 instead of throwing a null pointer exception in Java! Why?

Bhavesh
  • 4,607
  • 13
  • 43
  • 70
  • 1
    @see: http://stackoverflow.com/questions/1932625/java-return-class-not-an-instance – Martijn Courteaux Nov 07 '11 at 20:37
  • possible duplicate of [How come invoking a (static) method on a null reference doesn't throw NullPointerException?](http://stackoverflow.com/questions/3293353/how-come-invoking-a-static-method-on-a-null-reference-doesnt-throw-nullpointe) – nawfal Dec 16 '13 at 10:34

3 Answers3

12

As opposed to regular member variables, static variables belong to the class and not to the instances of the class. The reason it works is thus simply because you don't need an instance in order to access a static field.

In fact I'd say it would me more surprising if accessing a static field could ever throw a NullPointerException.

If you're curious, here's the bytecode looks for your program:

// Create TestNull object
3: new             #3; //class TestNull
6: dup
7: invokespecial   #4; //Method TestNull."<init>":()V

// Invoke the temp method
10: invokevirtual   #5; //Method TestNull.temp:()LTestNull;

// Discard the result of the call to temp.
13: pop

// Load the content of the static field.
14: getstatic       #6; //Field TestNull.field:I

This is described in the Java Language Specification, Section 15.11.1: Field Access Using a Primary. They even provide an example:

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

class Test {
        static String mountain = "Chocorua";
        static Test favorite(){
                System.out.print("Mount ");
                return null;
        }
        public static void main(String[] args) {
                System.out.println(favorite().mountain);
        }
}

It compiles, executes, and prints:

Mount Chocorua

aioobe
  • 413,195
  • 112
  • 811
  • 826
5

Static variables are shared among every object of a class. So while the actual object reference you are returning is null (In C++: TestNull* temp = null;), you have an object of type TestNull which Java can use to find static values of that class.

Remember in Java objects are really pointers. Pointers have a type. From that type Java can discern certain information, even if its pointing to null.

onit
  • 6,306
  • 3
  • 24
  • 31
3

Static fields are associated with the class not an instance of that class. Therefore you don't need an instance of an object to access the static field.

You could also access the field by calling TestNull.field

s1mm0t
  • 6,035
  • 4
  • 38
  • 45