Java - What is the output: initializes final variable in constructor call chain?

Question

What is the output of the following code?

class Test {

  private final int y;

  public Test() {
    this(20); 
    y = 10;
    System.out.println(y);
  }

  public Test(int z) {
    y = z; 
    System.out.println(y);
  }

}

public class Main {

  public static void main(String[] args) {
    new Test();
  }
}


Click to view the answer

y = 10;//The final field y may already have been assigned

Note

Both constructors initialize the blank final instance variable y.

No-args constructor calls another constructor.

Related Quiz