Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling static method in java [duplicate]

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

like image 773
Raj Avatar asked Jan 02 '13 16:01

Raj


Video Answer


1 Answers

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.

like image 96
PermGenError Avatar answered Oct 27 '22 10:10

PermGenError