b0b
b0b

Reputation: 33

cannot reference 'method' before superconstructor has been called

Lets say I have couple of classes and I am trying to call one of the methods in my super class and this error happens. I am not able to understand why?

File1:

public class A extends B {
   public A(int arg1){
       this(arg1, new class_C(getMethod()));  // Throws error here
   }
   public A(int arg1, int arg2){
       super(arg1) ;
  }
}

File2:

public class B{
   public B(int arg1){
       super(arg1) ;
  }
  public int getMethod() { return 100;}
}

Thanks for your help.

Upvotes: 0

Views: 67

Answers (2)

Eric
Eric

Reputation: 193

In java, the constructor must be called firstly. In your code, the getMethod in the B class is called before its' constructor is called. If this was allowed in java, it would messed up it's initialization.

Upvotes: 1

Eugene
Eugene

Reputation: 11075

So many error here.

  • What is the return type of getMethod in class B?
  • Why do you call super(arg1) in class B? Have you ever defined the parent class of class B?
  • Where is the definition of class_C?

You'd better provide a MCVE for your question.

Upvotes: 2

Related Questions