class A{
void display(){
System.out.println("hai");
}
}
class B {
static A a;
}
class C{
public static void main(String args[])
{
B.a.display();
// no compile- time error here.why?
}
}
Also I know that a will be set to null during runtime. But shouldnt the compiler know that we are accessing a non-static method via a static reference variable? This gives a null pointer exception when executed but why is the compiler not giving an error.
Does this mean a static reference variable behaves exactly like an object reference and thus can invoke any method(static and non-static) of the class?
Edit: I am basically confused with static field's access rules. By definition static fields can only directly access other static fields. So does this not include "invoking" a method using a static reference variable? And display() is accessed before its object is created. Is this valid?
areferences an instance ofA, why shouldn't you be able to call an instance method?