Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dan Chisholm exam - Topic Constructors

 
shweta mathur
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
I am trying to solve Dan Chisholm exam for "Constructors" but I am unable to follow most of the questions .
Can someone give a reference to some study material where I can know how Java treats such programs. Means- how and when static initializers and other statements in a block are called when instances of inherited classes are created.
I am giving one of such questions:
(This is the modified version of Q-2 of Dan's exam where I have removed the "j" in super to remove the error)
The output now is 261357 (Ans a).
My question is how do we get this output.

class Q {
int i = 1;
{System.out.print("1");}
static {System.out.print("2");}
Q() {System.out.print("3");}
Q(int x) {System.out.print("4");}
}
class R extends Q {
int j = 2;
{System.out.print("5");}
static {System.out.print("6");}
//R() {super(j);System.out.print("7");}// dan's question
R() {super();System.out.print("7");}// my question
}
class S {
public static void main(String[] args) {
new R();
}
}

What is the result of attempting to compile and run the program?
a. Prints: 261357
b. Prints: 261457
c. Prints: 267315
d. Prints: 267415
e. Compiler Error
f. Runtime Error
g. None of the Above
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shweta,
The resource that I used to develop the questions for the initialization topic was
Chapter 12 of the Java Language Specification.

The answer is "b. Prints: Static Main Constructor".
From the above we can see that the static initialization runs just after the class is loaded and before any static method is invoked. The constructor does not run until an instance of the class is created.

The answer is "e. Prints: Static Instance Constructor".
From the above we can see that the instance initializer runs before the body of the constructor.

The answer is "b. Prints: SA SB IA CA IB CB".
The remark associated with the answer is as follows.

Static initialization of the super class occurs first followed by static initialization of the sub class. Instance initialization of the super class occurs before instance initialization of the sub class.

I hope the above is helpful.
 
Alfred Kemety
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was also trying to follow it, it's a VERY good resource, I tried to summerize this bit of constructors as follows:
On instantiating an object of a certain type, the static initializers of the super classes are executed first starting with the highest class in the heirarchy, then the static initializer of the class of the object itself, then because of an implicit or explicit super() call the instance initializers and the constructors of the super classes are executed in couples (instance initializer & constructor) then at the end the inistance initializer and constructor of the class of the object itself are executed.
Hope this helps..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic