Skip to content

Can Java Invoke a Static Method From a Null Object

What's the output of the following program?

  • NPE?

  • 10?

Java
public class NullStaticMethod {

    public static void main(String[] args) {

        String str = null;

        System.out.println(str.valueOf(10));

    }

}
Expand to see anwser

The output is: 10

Even str is null, but Java still can invoke the status method defined in the String class.