How come invoking a (static) method on a null reference doesn't throw NullPointerException?

JavaStaticNullNullpointerexception

Java Problem Overview


I wrote this program in Java

public class Why {

  public static void test() {
    System.out.println("Passed");
  }

  public static void main(String[] args) {
    Why NULL = null;
    NULL.test();
  }

}

I read that invoking a method on a null object causes NullPointerException, and yet the above program doesn't? Why is this? Am I not understanding something correctly?

Java Solutions


Solution 1 - Java

test() is a static method. A static member belongs to the type, and do not require an instance to access.

A static member should ONLY be accessed via a type expression. That is, you should've written it as follows:

Why.test(); // always invoke static method on the type it belongs to!

Java does allow you to access a static member via an object reference expression, but this is VERY misleading, since this is NOT the actual semantics of a static member access.

Why aNull = null; 
aNull.test(); // DO NOT EVER DO THIS!
// invokes Why.test(), does NOT throw NullPointerException

When accessing a static member through an object reference expression, only the declared type of the reference matters. This means that:

  • It doesn't matter if the reference is actually null, since no instance is required
  • If the reference is not null, it doesn't matter what the runtime type of the object is, there is no dynamic dispatch!!!

As you can see, the exact opposites are true on both points for instance member access. This is why static members should NEVER be accessed in a "non-static" way, because it gives a very misleading appearance on what it's actually doing.

Solution 2 - Java

Static methods do not need a reference to the object. So you can call it even reference to the object is null. That's how main method works.

Try removing static designation from the object to see the null pointer exception.

Solution 3 - Java

It's a static method, which allows you to call methods on it without instantiating an instance.

Solution 4 - Java

You should turn on the various warnings in your IDE. Chances are you will see a warning about accessing a static member in a non-static way.

You could do something like (Why)(null).test(), it is only using the (Why)(null) to get the class.

Solution 5 - Java

Static Variable & Methods are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory.

Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class and the same apply for Static Method. for more refer this.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArtjemView Question on Stackoverflow
Solution 1 - JavapolygenelubricantsView Answer on Stackoverflow
Solution 2 - JavaVladView Answer on Stackoverflow
Solution 3 - JavaJohnFxView Answer on Stackoverflow
Solution 4 - JavaMrJacqesView Answer on Stackoverflow
Solution 5 - JavaRupesh YadavView Answer on Stackoverflow