aspose file tools*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructor" Watch "Constructor" New topic
Author

Constructor

sreelakshmi sarma
Ranch Hand

Joined: Mar 02, 2000
Posts: 130
Is constructor and the instance initializer same???
Thanks.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
No. They are not the same. Instance initializers are processed before constructors are invoked. Here instance initializers include the floating instance blocks, and initialization of instance var.Initialization of instance blocks and instance vars are done in the same order as they are written in the java source.
For eg.

regds
maha anna

[This message has been edited by maha anna (edited March 07, 2000).]
sreelakshmi sarma
Ranch Hand

Joined: Mar 02, 2000
Posts: 130
Thanks anna.
Jane Rozen
Ranch Hand

Joined: Feb 12, 2000
Posts: 53
Originally posted by maha anna:
No. They are not the same. Instance initializers are processed before constructors are invoked.

Are they?? I think it is vice versa.

[This message has been edited by Jane Rozen (edited March 10, 2000).]
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Jane,
Instance initializers are processed before invoking constructor is true. In the previous program I have given the output also. Please check by compiling and running whenever you get doubts like this.
regds
maha anna
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
Instance initializers are a handy artifact for factoring out code that should be in every constructor, saving you the extra trouble of chaining them with <CODE>this()</CODE>.
And Maha is right. These are executed prior to executing the Constructor.
Hey Maha: You are ready for the exam IMO!

Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Jane Rozen
Ranch Hand

Joined: Feb 12, 2000
Posts: 53
We were talking about INVOKING a constructor, not about EXECUTING it.

[This message has been edited by Jane Rozen (edited March 10, 2000).]
Rolf Weasel
Ranch Hand

Joined: Feb 26, 2000
Posts: 82
instance initializers are executed after all superclass constructors are executed and before the constructor of the current class is executed. in fact, execution of the current class constructor is started, and starts with a call to the super constructor or this() constructors. after returning from this call, instance initializers are executed.


May the Moose be with you.
Rolf Weasel
Ranch Hand

Joined: Feb 26, 2000
Posts: 82
in nitpicky mode, it is incorrect to say that instance initializers are executed before the constructor is evoked, since it is after the first line method call of the constructor returns ( or, when super() and this() is not present, after the implicit call to the default super constructor returns) that instance initializers are executed.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Rolf,
Let me tell my point clearly first. Every class whether it is a superclass to some class or a subclass to another class can have static initializers as well as instance initializers right? My point is, In each class the order of initialization is
1.All static initializers are executed from top to bottom of the inheritance hierarchy first.
2. Next From top to bottom , in each class the instance initialization and constructor code are executed at once and it goes down to next level.
3. The 2nd step is continued upto the bottom most class.
Don't confuse this concept. Because people who are not sure about this,can get easily confused by your replies.
To make my explanation clear I am posting this code and the output also.
regds
maha anna
<pre>

stat block test1 called
stat block test2 called
stat block test3 called
instance block test1 called
Constructor test1() called
instance block test2 called
Constructor test2() called
instance block test3 called
Constructor test3() called

class test1 {
static int stat11 =10;
static {
System.out.println("stat block test1 called");
}
int inst11 =20;
{
System.out.println("instance block test1 called");
}
test1() {
System.out.println("Constructor test1() called");
}
}
class test2 extends test1 {
static int stat21 =10;
static {
System.out.println("stat block test2 called");
}
int inst21 =20;
{
System.out.println("instance block test2 called");
}
test2(){
System.out.println("Constructor test2() called");
}
public static void main(String[] args) {
new test2();
}
}
class test3 extends test2 {
static int stat31 =10;
static {
System.out.println("stat block test3 called");
}
int inst31 =30;
{
System.out.println("instance block test3 called");
}
test3() {
System.out.println("Constructor test3() called");
}
public static void main(String[] args) {
new test3();
}
}
</pre>

[This message has been edited by maha anna (edited March 10, 2000).]
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
Out of nitpicking mode, what is important is that instance initializers are executed before the body of the constructor is. This is important, since if you initialize a variable in an instance initializer and alter it again inside the constructor, it will contain the last value, which was assigned in the constructor.
The complete steps in object initialization are:
Instance variables are initialized to their default values and then the constructor is invoked. This can lead to local chaining of constructors. The invocation of the constructor at the end of the local chain results in the following actions before the constructor's execution resumes:
Invocation of the superclass's constructor implicitly or explicitly.
Initialization of the instance member variables by executing their instance initializers expressions and any instance initializer blocks in the order they are specified in the class definition.
Jim Crawford
Ranch Hand

Joined: Sep 08, 2002
Posts: 127
Originally posted by Tony Alicea:
...
Initialization of the instance member variables by executing their instance initializers expressions and any instance initializer blocks in the order they are specified in the class definition.

...and then the execution of the rest of the constructor body continues.

I have to refile this sequence - been a while since I've studied it last.
ps. You are leaving out the static bit in the beginning, but I suppose Maha explained it well enough above.
Cheers.
[ January 15, 2004: Message edited by: Jim Crawford ]

<img src="cool.gif" border="0"> <img src="graemlins/beerchug.gif" border="0" alt="[beerchug]" /> <br />SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor