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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
adrian110288
Posts: 12
Joined: Thu Aug 01, 2013 3:44 pm
Contact:

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

Post 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:

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

It can get more complicated if the blocks mentioned call polymorphic methods. Or if the field initialization involves method calls.

HTH,
Paul.

shining_dragon
Posts: 14
Joined: Sat Mar 01, 2014 9:12 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

In the order of appearance. Please check Java Language Specification for more details.

AndaRO
Posts: 31
Joined: Wed Feb 08, 2017 5:42 pm
Contact:

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

Post by AndaRO »

I am expected to be executed all from base class and then all from class.

Kevin30
Posts: 28
Joined: Sun Oct 25, 2015 10:14 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

Kevin30
Posts: 28
Joined: Sun Oct 25, 2015 10:14 am
Contact:

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

Post by Kevin30 »

Excellent explanation, I get it now! Thank you!

Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests