Consider the following Java class declaration:
public class Test {
private final int defaultValue = 10;
private int var;
public Test() {
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;
}
}