The below code is getting compiled without any errors inspite of accessing the method dir
from a null object rt
, whereas i was expected to get nullpointerexception
. Why?
When i removed the static keyword for dir method, i got the null pointer exception, so is there any exception for static methods?
public class root{
private root() {}
final public static void dir(int a)
{
System.out.print("Output: "+a);
}
}
public class plan{
public root rt=null;
public void plot(){
rt.dir(1); //Calling a static method using null object
}
public void static main(String[] args){
plan p1=new plan();
p1.plot();
}
}