The following simple code snippet is working fine and is accessing a static field with a null object.
final class TestNull
{
public static int field=100;
public TestNull temp()
{
return(null);
}
}
public class Main
{
public static void main(String[] args)
{
System.out.println(new TestNull().temp().field);
}
}
In the above code, the statement System.out.println(new TestNull().temp().field);
in which the static field field is being associated with a NULL objectnew TestNull().temp()
, still it is returning a correct value of it which is 100 instead of throwing a null pointer exception in Java! Why?