Initialize field before super constructor runs?
Asked Answered
L

7

46

In Java, is there any way to initialize a field before the super constructor runs?

Even the ugliest hacks I can come up with are rejected by the compiler:

class Base
{
    Base(String someParameter)
    {
        System.out.println(this);
    }
}

class Derived extends Base
{
    private final int a;

    Derived(String someParameter)
    {
        super(hack(someParameter, a = getValueFromDataBase()));
    }

    private static String hack(String returnValue, int ignored)
    {
        return returnValue;
    }

    public String toString()
    {
        return "a has value " + a;
    }
}

Note: The issue disappeared when I switched from inheritance to delegation, but I would still like to know.

Leggett answered 28/3, 2013 at 12:58 Comment(11)
are you trying to pre-initialize field a?Resinous
I don't think you can do this. Any initialization you do in a class (even if it is outside the constructor) is moved to every constructor after the super call. So, the super constructor is always run before the field initialization.Arri
@FredOverflow since a is only accessible in Derived, why does it matter that it gets initialised before super() is called? Initialising it right after does not make a difference in the example your provide (unless you call an overriden method from the Base constructor, which begins to smell quite fishy).Byproduct
Effective Java Item 17: "Constructors must not invoke overridable methods, directly or indirectly (...) If the overriding method depends on any initialization performed by the subclass constructor, the method will not behave as expected."Byproduct
can a final variable be initialized in the constructor?Chum
@codeMan. Yeah of course. Look for Immutable classes.Arri
@RohitJain Din't know that! Thanks.Chum
Ugly Hack: Create the Derived class directly in java bytecode as in this answer: #3279365Radarman
@Byproduct Actually, it does make a difference in my example, because System.out.println(this) internally calls toString(), which is overriden to print the value of a.Leggett
@FredOverflow Yes that was my point - it is the only situation where it makes a difference and one which you probably want to avoid.Byproduct
I think this one has a nicer solution that can be used for this as well: #2304104, Maybe instead of a hack() function you'll need to do a Hack class, and then you can really use the solution in the linkPorta
L
34

No, there is no way to do this.

According to the language specs, instance variables aren't even initialized until a super() call has been made.

These are the steps performed during the constructor step of class instance creation, taken from the link:

  1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.
  2. If this constructor begins with an explicit constructor invocation (§8.8.7.1) of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.
  3. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.
  4. Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5.
  5. Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.
Lucre answered 28/3, 2013 at 13:5 Comment(1)
@mkilic: Pretty sureOverleap
D
16

Super constructor will run in any case, but since we are talking about the "ugliest hacks", we can take advantage of this

public class Base {
    public Base() {
        init();
    }

    public Base(String s) {
    }

    public void init() {
    //this is the ugly part that will be overriden
    }
}

class Derived extends Base{

    @Override
    public void init(){
        a = getValueFromDataBase();
    }
} 

I never suggest using these kind of hacks.

Dolf answered 28/3, 2013 at 13:19 Comment(3)
+1 - You're right. Ick. I was thinking of C++. Still -- I don't think the hack is necessary - see my answer.Tabethatabib
This isn't really before the super constructor runs, as OP asked. Probably the closest you can get though.Lucre
@Lucre I mentioned in the answer "Super constructor will run in any case", this just a hack to initialize a variable in the derived classWillms
F
9

I got a way to do this.

class Derived extends Base
{
    private final int a;

    // make this method private
    private Derived(String someParameter,
                    int tmpVar /*add an addtional parameter*/) {
        // use it as a temprorary variable
        super(hack(someParameter, tmpVar = getValueFromDataBase()));
        // assign it to field a
        a = tmpVar;
    }

    // show user a clean constructor
    Derived(String someParameter)
    {   
        this(someParameter, 0)
    }

    ...
}
Festinate answered 6/9, 2013 at 12:46 Comment(0)
T
8

As others have said, you can't initialize the instance field before calling the superclass constructor.

But there are workarounds. One is to create a factory class that gets the value and passes it to the Derived class's constructor.

class DerivedFactory {
    Derived makeDerived( String someParameter ) {
        int a = getValueFromDataBase();
        return new Derived( someParameter, a );
    }
}


class Derived extends Base
{
    private final int a;

    Derived(String someParameter, int a0 ) {
        super(hack(someParameter, a0));
        a = a0;
    }
    ...
}
Tabethatabib answered 28/3, 2013 at 13:17 Comment(1)
This is the best solution in my opinion. The only thing I would change is that I would put a static factory method inside the Derived class.Culpa
H
1

It's prohibited by the Java language specification (section 8.8.7):

The first statement of a constructor body may be an explicit invocation of another constructor of the same class or of the direct superclass.

The constructor body should look like this:

ConstructorBody:

{ ExplicitConstructorInvocationopt BlockStatementsopt }
Hemichordate answered 28/3, 2013 at 13:6 Comment(0)
B
0

although it's not possible to do it directly, you can try to do it with nested object

so with your example:

open class Base {
    constructor() {
        Timber.e(this.toString())
    }
}

class Derived {
    val a = "new value"

    val derivedObject : Base =  object : Base() {
        override fun toString(): String {
             return "a has value " + a;
        }
    }
}

Happy coding - it's hack but works :) remember to define derivedObject as LAST variable

Boyce answered 23/7, 2020 at 10:15 Comment(0)
K
0

I designed a hierarchy of UI forms which ran into a similar issue. I found a similar case and some workarounds here: https://www.javaspecialists.eu/archive/Issue086-Initialising-Fields-Before-Superconstructor-Call.html

I changed a little the implementation of their first workaround to make it more generic (by accepting a variable number of parameters):

public abstract class BaseClass {
    int someVariable; // not relevant for the answer
    
    public BaseClass (int someVariable, Object... arguments) {
        this.someVariable = someVariable;
        
        // Must be called before using local variables of derived class
        initializeLocalVariablesOfDerivedClass(arguments);
        
        useLocalVariablesOfDerivedClass();
    }
    
    protected abstract void initializeLocalVariablesOfDerivedClass (Object... arguments); 
    
    protected abstract void useLocalVariablesOfDerivedClass();
        
}

public class DerivedClass extends BaseClass {
    protected String param1;
    protected Integer param2;
    
    public DerivedClass(int someVariable, Object... arguments) {
        super(someVariable, arguments);
    }

    @Override
    protected void initializeLocalVariablesOfDerivedClass(Object... arguments) {
        // you must know the order and type of your local fields
        param1 = (String) arguments[0];
        param2 = (Integer) arguments[1];        
    }
    
    @Override
    protected void useLocalVariablesOfDerivedClass() {
        System.out.println("param1: " + param1);
        System.out.println("param2: " + param2);                
    }
    
    public static void main(String[] args) {        
        new DerivedClass(1, new String("cucu"), new Integer("4"));
    }
}
Kukri answered 19/4, 2022 at 11:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.