Compiler Error J0148

Cannot reference instance method 'identifier' before superclass constructor has been called

The compiler detected an attempt to reference an instance method before the superclass constructor was called. This error usually occurs when a base class method is called from within a subclass's constructor using the super() statement. This error can also occur if the subclass calls its own methods from the constructor using the this() statement. This error occurs because instances of the subclass and base class have not been instantiated at the time the constructor is called. To avoid this situation, use the super. statement to call a base class method, and use the this. statement to call a subclass method.

The following example illustrates this error:

abstract class Simple {
   
   Simple(int i) {}
   
   int method1() {
      
      return 0;
   }
}

class SimpleSubclass extends Simple {
   
   SimpleSubclass() {
      
      super(method1());
      // error: constructor must be called first
   }
}

Using the abstract class from the example above, the following example illustrates how to call the base class method from the constructor:

class SimpleSubclass extends Simple{
   
   SimpleSubclass(){
     super.method1();
   }
}