Author Topic:   Constructor
sree
ranch hand
posted March 07, 2000 08:36 AM             
Is constructor and the instance initializer same????

Thanks.

maha anna
bartender
posted March 07, 2000 09:12 AM             
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.
code:

class Test {
int instVar=callMe();
//
{
System.out.println("inst block 1");
}
{
System.out.println("inst block 2");
}
//
Test() {
System.out.println("Test() constructor");
}
//
int callMe() {
System.out.println("maha");
return 10;
}
//
public static void main(String[] args) {
new Test(); /* prints maha
inst block1
inst block2
Test() constructor
*/
}
}


regds
maha anna

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

sree
ranch hand
posted March 07, 2000 09:35 AM             
Thanks anna.

Jane Rozen
ranch hand
posted March 10, 2000 02:16 PM             
quote:
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
bartender
posted March 10, 2000 02:21 PM             
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
sheriff
posted March 10, 2000 04:26 PM             
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 this().

And Maha is right. These are executed prior to executing the Constructor.

Hey Maha: You are ready for the exam IMO!

Jane Rozen
ranch hand
posted March 10, 2000 05:42 PM             
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
posted March 10, 2000 08:31 PM             
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.

Rolf Weasel
ranch hand
posted March 10, 2000 08:38 PM             
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
bartender
posted March 10, 2000 09:21 PM             
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


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();
}
}


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

Tony Alicea
sheriff
posted March 10, 2000 10:15 PM             
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.

|