Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1091 :

Posted: Sun Sep 01, 2013 1:37 pm
by adrian110288
In the explanation it is said that
Although there is more to it that the following sequence, for the purpose of exam, this is all you need to know: ...
. What is meant by that? I am just curious :roll:

Re: About Question enthuware.ocajp.i.v7.2.1091 :

Posted: Sun Sep 01, 2013 2:54 pm
by admin
It can get more complicated if the blocks mentioned call polymorphic methods. Or if the field initialization involves method calls.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1091 :

Posted: Mon Mar 10, 2014 9:57 am
by shining_dragon
a little bit OT, what is the order if there are non-block (static and instance fields) declared within the class? What would be the order of execution then?

Re: About Question enthuware.ocajp.i.v7.2.1091 :

Posted: Mon Mar 10, 2014 11:06 am
by admin
In the order of appearance. Please check Java Language Specification for more details.

Re: About Question enthuware.ocajp.i.v7.2.1091 :

Posted: Thu Mar 02, 2017 1:53 pm
by AndaRO
I am expected to be executed all from base class and then all from class.

Re: About Question enthuware.ocajp.i.v7.2.1091 :

Posted: Wed Aug 23, 2017 9:40 am
by Kevin30
I don't get it.

So this is the code in the question:

Code: Select all

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 rules I know with regard to the order of execution of instance initialization & static initialization blocks are as follows (from Sierra & Bates' OCA Java SE 7 Programmer I Study Guide, p139):

1) Initialization blocks execute in the order in which they appear
2) Static initialization blocks run once, when the class is first loaded
3) Instance initialization blocks run every time a class instance is created
4) Instance initialization blocks run after the constructor's call to super()

From making the Enthuware questions, I deducted an additional rule:
5) In the execution of code, there is no hierarchical difference between static statments & static initialization blocks, only the order in which they appear matters. The same goes for instance statements & instance initialization blocks, there is no hierarchical difference between them either.

So if I follow the rules, then this is what should be printed:
Manager static Employee static 10 Employee 15 Manager 30.

It should go as follows:

A) new Manager() in the main-method calls the default constructor in the Manager class. Therefore, the Manager-class is loaded and its static initialization block runs (see rule 2). It should print out: "Manager static" .
[Note that this is not a static initialization block so rule 4 doesn't apply.]
B) Then super() is called and class Employee is loaded and its static initialization block runs (rule 2). There is no hierarchical difference in the execution of code between static statements & static initialization blocks (rule 5) [so whichever static statement or static initialization block that appears first, also runs first. So static int i first gets the value 10, and then the static initialization block runs, so it prints out: "Employee static 10".
C) Then super() in Object() gets called and then control comes back to class Employee. Now rule 4 (and 5) go into effect: Instance initialization blocks run after the constructor's call to super(), so the instance initialization block gets printed (and i gets value 15). It prints out: "Employee 15".
D) Then control goes back to the class Manager. Rule 4 (and 5) go into effect): Instance initialization blocks run after the constructor's call to super() so "Manager 30" gets printed.

What am I doing wrong? Please help me understand.

Re: About Question enthuware.ocajp.i.v7.2.1091 :

Posted: Wed Aug 23, 2017 10:33 pm
by admin
Well, when you do Manager m = new Manager(), the JVM will try to load Manager class but since Manager extends Employee, it will need to load Employee before loading Manager. While loading Employee class, it will execute the static statement and the static block in Employee. That is why "Employee static 10" will be printed first.

Once Employee class is loaded, Manager's static block will be executed and so "Manager static" will be printed.

The same logic applies for instance blocks as well. To create an instance of Manager, the JVM needs to first initialize the Employee, and that is why Employee's instance blocks will be executed before Manager's. Therefore, it prints "Employee 15" and then "Manager 30"

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1091 :

Posted: Thu Aug 24, 2017 10:37 am
by Kevin30
Excellent explanation, I get it now! Thank you!