Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Java compiler allow static variable access through null object? [duplicate]

I was pointing some tricks and came across this. In following code:

public class TestClass1 {      static int a = 10;      public static void main(String ar[]){         TestClass1 t1 = null ;         System.out.println(t1.a); // At this line     } } 

t1 object is null. Why this code is not throwing NullPointerException?

I know this is not proper way to access static variables but question is about NullPointerException.

like image 650
Not a bug Avatar asked Jan 10 '14 06:01

Not a bug


People also ask

Can a null reference be used to access a static variable?

Since static members belongs to class rather than instance. A null reference may be used to access a class (static) variable without causing an exception.

Why static variable is used in Java?

The static keyword in Java is mainly used for memory management. The static keyword in Java is used to share the same variable or method of a given class. The users can apply static keywords with variables, methods, blocks, and nested classes. The static keyword belongs to the class than an instance of the class.

Why static is always single copy?

Only one copy of a static member exists, regardless of how many instances of the class are created. Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter.

When static variables loaded in memory in Java?

The static fields are loaded when the class is loaded. This usually happens which the file object of a class is created, but it can be earlier if the class is used another way.


2 Answers

There is no need for an instance while invoking static member or method.

Since static members belongs to class rather than instance.

A null reference may be used to access a class (static) variable without causing an exception.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#d5e19846

If you see the example (see full example in spec)

 public static void main(String[] args) {         System.out.println(favorite().mountain); //favorite() returns null     } 

Even though the result of favorite() is null, a NullPointerException is not thrown. That "Mount " is printed demonstrates that the Primary expression is indeed fully evaluated at run time, despite the fact that only its type, not its value, is used to determine which field to access (because the field mountain is static).

like image 137
Suresh Atta Avatar answered Sep 19 '22 04:09

Suresh Atta


To add some additional info to the current answers, if you disassemble your class file using:

javap -c TestClass1 

You'll get:

Compiled from "TestClass1.java" public class TestClass1 extends java.lang.Object{ static int a;  public TestClass1();   Code:    0:   aload_0    1:   invokespecial   #1; //Method java/lang/Object."<init>":()V    4:   return  public static void main(java.lang.String[]);   Code:    0:   aconst_null    1:   astore_1    2:   getstatic   #2; //Field java/lang/System.out:Ljava/io/PrintStream;    5:   aload_1    6:   pop    7:   getstatic   #3; //Field a:I    10:  invokevirtual   #4; //Method java/io/PrintStream.println:(I)V    13:  return  static {};   Code:    0:   bipush  10    2:   putstatic   #3; //Field a:I    5:   return } 

Here you can see that the access to the static field is done in line 7 by the getstatc instruction. Whenever a static field is accessed through code, a corresponding getstatic instruction will be generated in the .class program file.

*static instructions have the particularity that they don't requiere a reference to the object instance to be in the stack prior to calling them (like, for example invokevirtual which does require an object ref in the stack), they resolve the field/method using just an index to the run time constant pool that will be later used to solve the field reference location.

That's a technical reason for the warning "The static field should be accessed in a static way" that some IDEs will throw at you when you write t1.a, because the object instance is unnecessary to resolve the static field.

like image 29
higuaro Avatar answered Sep 20 '22 04:09

higuaro