This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructor" Watch "Constructor" New topic
Author

Constructor

suresh kamsa
Ranch Hand

Joined: Jul 30, 2001
Posts: 149
class D {
{System.out.print("1");}
static {System.out.print("2");}
D() {System.out.print("3");}
D(String s) {System.out.print("4");} // l3
}
class E extends D {
{System.out.print("5");}
static {System.out.print("6");}
// E() {System.out.print("7");this("e");} // l1
E() {this("e");System.out.print("7");} // l2
E(String s) {System.out.print("8");}
}
class F {
public static void main(String[] args) {
new E();
}
}
If I comment l1 its compile error free and
1.output is 2613587?
2.Can anyone run me through why the output is 2613587?
3.Why compile error at l1?
4. Why is it not executing l3?
Thanks
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
Hi Suresh,
For your ques 3 -
JLS 15.8.3 -
.........The keyword this is also used in a special explicit constructor invocation statement, which can appear at the beginning of a constructor body.
If this ( & also super ) is to be present in the body of a constructor it should be the first statement.
For your ques 1 & 2
Now , all static things are taken into account during class load time. So when E is first loaded it'll take up all the static thingy's. Further since E is a subclass of D it'll first need to load D. So first load D then load E & then you first create D & next you create E. Here's how it'll go -
static initializer of D - 2
static initializer of E - 6
instance initializer of D - 1
creation of D - 3
instance initializer of E - 5
creation of E - this calls E with String args - 8
next line in E's no-args constructor - 7
For your ques 4 -
JLS 8.8.5 Constructor Body -
.........If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body is implicitly assumed by the compiler to begin with a superclass constructor invocation "super();", an invocation of the constructor of its direct superclass that takes no arguments.
So the D with String args never gets called
HTH
Ashish H.
[ September 10, 2002: Message edited by: Ashish Hareet ]
Alan Phillips
Ranch Hand

Joined: Aug 13, 2002
Posts: 73
So, if I'm getting this right, the order goes like this:
static initializers
class initializers
constructors (super() always called first if this() or super() not called in the first line of the constructor)
correct?
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
So, if I'm getting this right, the order goes like this:
static initializers
class initializers
constructors (super() always called first if this() or super() not called in the first line of the constructor)
correct?

Loosely said , yes.
static initializers won't be called more than once & super classes will always be called before subclasses.
See JLS 12.4 & 12.5
Correct me if I am wrong
Ashish H.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
A. When classes are loaded: (before objects are created:
1. static initializer of super class (recursive)
2. static initializer of derived class
B. When object is created:
1. constructor code of derived class is
launched:
a. if there is no explicit call for a super
class constructor (or a call to another
constructor of derived class), an implicit
call super()is placed. If there is a call
for super/derived class constructor, it has
to be first statement. This behavior is
repeated in all constructors. So if another
constructor of derived class is called with
this(string s), for example, that constructor
will call super() constructor. So one way or
other, super() gets called.

i. super constructor will call its own
super class constructor (recursive)
ii. instance initializers of super class
iii. code in super class constructor.
c. instance initializers of derived class
b. rest of the code in derived class
constructor.
[ September 10, 2002: Message edited by: Barkat Mardhani ]
 
 
subject: Constructor
 
Similar Threads
Q5+Q6 Dan
runtime vs compiler error
SCJP 5: Mock questions
One more on Constructor
Question from Dan's mock Exam