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
.
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.
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.
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.
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With