简体   繁体   中英

Cannot reference “X” before supertype constructor has been called, where x is a final variable

Consider the following Java class declaration:

public class Test {

    private final int defaultValue = 10;
    private int var;

    public Test() {
        this(defaultValue);    // <-- Compiler error: cannot reference defaultValue before supertype constructor has been called.
    }

    public Test(int i) {
        var = i;
    }
}

The code will not compile, with the compiler complaining about the line I've highlighted above. Why is this error happening and what's the best workaround?

The reason why the code would not initially compile is because defaultValue is an instance variable of the class Test , meaning that when an object of type Test is created, a unique instance of defaultValue is also created and attached to that particular object. Because of this, it is not possible to reference defaultValue in the constructor, as neither it, nor the object have been created yet.

The solution is to make the final variable static :

public class Test {

    private static final int defaultValue = 10;
    private int var;

    public Test() {
        this(defaultValue);
    }

    public Test(int i) {
        var = i;
    }
}

By making the variable static , it becomes associated with the class itself, rather than instances of that class and is shared amongst all instances of Test . Static variables are created when the JVM first loads the class. Since the class is already loaded when you use it to create an instance, the static variable is ready to use and so can be used in the class, including the constructor.

References:

It is because the defaultValue is an member of Test 's instance which is under construction (not yet created)

If you had it static it were loaded when your class loads by classloaders

You are referencing to a variable that doesn't exist yet , if it was static it would exist even before the constructor itself .

But you will face another problem, as defaultValue became static, so all other instances may share the same value which you may don't like it to be:

public class Test {

    private final int defaultValue = 10; //this will exist only after calling the constructor
    private final static int value2= 10; //this exists before the constructor has been called
    private int var;

    public Test() {
       // this(defaultValue);    // this method will not work as defaultValue doesn't exist yet
    this(value2); //will work
    //this(10); will work
    }

    public Test(int i) {
        var = i;
    }
}

Rule : Every constructor must execute super class's constructor before executing itself.

So the first line of every constructor is super() or may be this() and you are send the defaultValue to the this class constructor which(defaultValue) is not existed yet hence there is compile time error.

You can made defaultValue as static and since static variable is created as class is loaded to memory so defaultValue is available at the line this(defaultValue).

在你的对象没有被构造之前,变量的默认值不会被设置,因此如果你想在构造时设置它们的默认值,让它们static或在之前显式设置它们。

在创建对象时调用构造函数,因此编译器无法识别对变量的引用,因为编译器不了解实例变量,因为尚未创建对象。

Actually this is not correct answer, as during the creation of object, fields initialization instructions execute before the constructor. You can debug the object creation process and see that on your own. I myself am confused about this problem as well.. for example, if you will change a bit and first constructor will be:

public Test(int i) {
   this(i, 0);
}
public Test (int a, int k) {
}

This will work.. so, when the first/null constructor calls another, it is not working for a strange reason, even if I explicitly call super(); before.

The most relevant explanation would be, that JVM loads declarations in the memory, but NO CONSTRUCTOR IS ABLE TO REACH ANY INSTANCE VARIABLE/FIELD before it gets executed entirely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM