Call a static method on a null object

I am curious to know how would you explain this problem that I found in this quiz ? Even when the getFoo method returns null , the output is still Getting Object JavaQuiz . I think this should be a NullPointerException .

  public class Foo { static String name = " JavaQuiz"; static Foo getFoo() { System.out.print("Getting Object"); return null; } public static void main(String[] args) { System.out.println(getFoo().name); } } 
+5
source share
3 answers

Access to a static method or variable can be done using a null reference for a class that contains this static method / variable.

Since name is static, getFoo().name has the same result as Foo.name or just name , regardless of whether getFoo() returns null .

However, it is always better to use the class name when accessing the static method / variable, as it makes it clear that you intended to access the static member.

+11
source

static elements from the class are members of the class level, so it does not need an Object to access these static members.`

It loads automatically when the classloader from jvm loads the class. So here in this case

 static String name = " JavaQuiz"; //load when class get loaded by JVM class loader. 

This static variable will exist in memory right after the Foo class is loaded into jvm .

 static Foo getFoo() { //method is also a static Member automatically loaded at Class Loading. System.out.print("Getting Object"); return null; } 

And the same will apply with this static getFoo() method.

So here is System.out.println(getFoo().name); . Here is an example to abstract my answer

 class StaticExample { static String abc ="India"; public static void main (String[] args) throws java.lang.Exception { StaticExample obj = null; System.out.println("Value is ==>" + obj.abc + StaticExample.abc + abc); } } 

Output: -

  Value is ==>IndiaIndiaIndia 

here this line of code will also output the result.

  System.out.println(((Ideone)null).abc); // this will also print India. 

Output: -

  Value is ==>India 

Note. - I think there is confusion regarding the getFoo() method, but if .name does ambiguity. name is a static member, so it can be accessed using either className or any null reference. So, here you can consider this scenario such that this variable name can be obtained using any null reference.

Hope you make sense.

+2
source

Disassembling (javap -c Foo) bytecode of the Foo.java file shows information under the hood about why you don't see a NullPointerException when name is static .

With the above code, when it is parsed, it produces the following output.

with static field

If we look at the yellow field, we will see that the compiler identifies that we are trying to access the static field, and therefore it puts the getstatic statement to get the name field. Thus, it does not effectively use the instance returned from the getFoo method to get the value of the name field, and therefore no NPE is created.


If we remove the static before String name = " JavaQuiz"; , this will result in disassembled code.

No static keyword

Here we see that the java compiler instructs to use the getfield , and this will be called on the instance, that is, returned from the getFoo method. Thus, this will increase NPE if the getFoo method returns null .

Thus, in this case, the java compiler does the magic at compile time, i.e. if the code calls a static field, it places a getstatic statement that does not result in an object reference being used.

We can find more information on the instruction set here .

0
source

Source: https://habr.com/ru/post/1246325/


All Articles