Why does this method call on a null object run without a NullPointerException?

aryan :

What is the reason for the output? I know it prints Hello World but don’t know why as it should give NullPointerException.

public class Null
{
    public static void greet()
    {
        System.out.println("Hello World");

    }
    public static void main(String[] args)
    {
        ((Null)null).greet();
    }
}
Code_Mode :

This is because greet()is a static method. So

((Null)null).greet();

is equivalent to,

Null.greet()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=138529&siteId=1