1

I'm getting ready for OCJP. and in one question they asked what this code will print: I thought this code would not compile. (I have never seen this syntax. I'm self taught). can someone please explain this code to me? and why does it print?

class Employee {

  static int i = 10;  {
      i = 15;
      System.out.print(" Employee "+i);
  }
  static { System.out.print(" Employee static "+i); }    
}

class Manager extends Employee { 
  static {
    i = 45;
    System.out.print(" Manager static ");
  }{
    i = 30;
    System.out.print(" Manager "+i);
  }
}

class Owner extends Manager{
  static { System.out.println("Owner"); }
}

public class TestClass {    
  public static void main(String[] args) {
      Manager m = new Manager();
  }

the output:

Employee static 10 Manager static Employee 15 Manager 30
pythwan
  • 99
  • 9
  • 2
    The `{...}` are initialization blocks, read up more about it [here](https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html) – Nicholas K Mar 14 '20 at 16:02

0 Answers0