Java Software Engineer Question:

Download Job Interview Questions and Answers PDF

Explain me why doesn’t the following code generate a NullPointerException even when the instance is null?

Java Software Engineer Interview Question
Java Software Engineer Interview Question

Answer:

Test t = null;
t.someMethod();

public static void someMethod() {
...
}

There is no need for an instance while invoking a static member or method, since static members belongs to a class rather than an instance.

A null reference may be used to access a class (static) variable without causing an exception.

Download Java Software Engineer Interview Questions And Answers PDF

Previous QuestionNext Question
Explain me what is a good usecase of calling System.gc()?Do you know what is the advantage of generic collection?