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