Possible Duplicate:
How come invoking a (static) method on a null reference doesn’t throw NullPointerException?
Can any one explain why the output of the following program is "Called"
public class Test4{
public static void method(){
System.out.println("Called");
}
public static void main(String[] args){
Test4 t4 = null;
t4.method();
}
}
I know we can call static method with class reference , but here I am calling using null reference . please clarify my doubt
In the Byte code
Test4 t4 = null;
t4.method();
will be
Test4 t4 = null;
Test4.method();
Compiler would convert the call with the class name for static methods. refer to this question on SO which i myself have asked it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With