Look closely, print()
is a static method. This means it can be invoked without an instance of Test1
. I.e. it can simply be called as:
Test.print();
The fact that the test()
method returns null is irrelevant. In fact, if you're using a modern IDE it will probably have a warning on your invocation of test().print()
warning you that you're trying to invoke a static method on an instance of an object.
There is no NullPointerException because the JVM is not trying to deference the object returned by test(). The JVM knows that it does not need to invoke a static method on an instance of an object.
If you want to know more about the underlying implementation, and the difference between invokespecial
and invokestatic
I'd suggest this question.