Java - What is the output: initializes final variable?

Question

What is the output of the following code?

class Test {
  private final int y;

  {
    y = 10; 
    System.out.println("1");
  }

  {
    y = 10; 
    System.out.println("2");
  }
  {
    System.out.println("hi");
  }
}

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
    System.out.println("2");
}

Note

The code would not compile because it initializes y more than once inside two instance initializers:

Related Quiz