Java - What is the output: initializes final variable in an instance initializer?

Question

What is the output of the following code?

class Test {
  private final int y;

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

public class Main {

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


Click to view the answer

10
hi

Note

The following code will compile because it initializes y in an instance initializer:

Related Quiz