2
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?

5
  • 3
    I don't understand the question. If a references an instance of A, why shouldn't you be able to call an instance method? Commented Oct 4, 2018 at 20:46
  • When in separate classes, this will work (why not? ). But when these classes are subclasses (non-static subclasses), you wouldn't be able to declare a static variable in the non-static class in the first place. My guese is that this mechanism is confusing you atm? Commented Oct 4, 2018 at 20:51
  • So static reference variables can "invoke" any method. They only cant directly "access" non-static right? I assumed we cant invoke non-static methods as the variable is static. Commented Oct 4, 2018 at 20:57
  • No, a static variable (reference) could invoke any member of the class. It's just a normal reference, like any other reference. So an example for us so we can see what it is you are talking about. Commented Oct 4, 2018 at 21:27
  • There's a couple of things you might be confused over. Local variables have to be definitely initialized, and the compiler can tell if they've been initialized because they're local. You'll get a compiler error if you try to use a local variable that hasn't been initialized yet. The other is referring to an instance member directly (not through a reference) from a static method (or other static context). Those are also definitely errors, and the compiler will flag them. Commented Oct 4, 2018 at 21:30

1 Answer 1

1

"You keep using that word. I do not think it means what you think it means."

The static A a; in your code says, "a is a (reference to) an object of type A that is shared across all instances of class B. a is uninitialized.".

The compiler will have no context to determine when you are going to call the static main() method of class C, so it can't tell what the state of variable a will be at the time you call C.main();. In fact, you could just as easily have a class D:

class D
{
    public static void main( String args[] )
    {
        B.a = new A();
        C.main( args );
    }
}

which would make the code above completely valid and runnable.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes I added the extends A by mistake . Corrected my question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.