• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dan Chisholm exam - Topic Constructors

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic