What is the output of the following code? “`java public class Test { static int x = 10; static { x = 20; } static { x = x + 5; } public static void main(String[] args) { System.out.println(x); } }“`

Java Professional Hard

Java Professional — Hard

What is the output of the following code? “`java public class Test { static int x = 10; static { x = 20; } static { x = x + 5; } public static void main(String[] args) { System.out.println(x); } }“`

Key points

  • Static blocks are executed before the main method in Java.
  • The value of x is updated in each static block.
  • The final value of x is 25 after both static blocks are executed.

Ready to go further?

Related questions