This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

initialization order

 
leena rane
Ranch Hand
Posts: 280
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
4.
The order:
Once a class's base class cstr has completed executing,
any instance initializers defined by the class are executed,
(in the order in which they are defined.)
After all instance initializer have executed, the cstr body finally executes.
is this true?
I used tp think the order 2 b
1.instance variables
2.the instance initializers
3.base class constructor
4.subclass constructor
Is the above explanation trying to say 3 is before 1?
Thanx in advance
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember the base class initialization must be completed before beginning the initialization of the subclass. But in your sequence the fields of the subclass are initialized before the base class. If they were using some base class fields what value they will obtain?
The compiler gathers all the variables initializers and instance initializers in a single one. They are gathered in the textual order they appear.
This resulting code is placed, in a certain place, in any constructor that does't begin with some form of this(). The place is just after the impicit or explicit invocation of super, but before the rest of the code in the constructor. In this way the initialization of the fields of a class is made only after the returning of super. That is, after after the initialization of all of its superclasses.
You can check this with javap -c YourClass
 
leena rane
Ranch Hand
Posts: 280
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx a lot Jose
(To be very frank , i don't consider you as a ranch hand - but a bartender )
Just help me with javap
When i use
javap -c sup -> i get output
but when i use
javap -c sub -> i get class ' sub not found'
why?

Thanx in advance
 
Nain Hwu
Ranch Hand
Posts: 139
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried both. I had no problem getting output in my environment (JDK1.3.1
in windows 95)
[This message has been edited by Nain Hwu (edited October 20, 2001).]
 
leena rane
Ranch Hand
Posts: 280
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hav jdk1.2.1 ,win98
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
I think the order that is followed in class initialization ( in the above program) is ...
1. The print statement is executed.
2. The subclass constructor is called.

3. But, even before executing the first line in the constructor ( that is, printing " sub constructor entered "), it calls the superclass constructor as expected.
4. Now, the control passes on to the super class constructor. Here, too, even before printing " sup constructor entered ", it takes care of int isup=10;int jsup=11; and then the initialization block.
5. After this has been executed, it now prints " sup constuctor entered" and goes through the routine in the constructor.
6. Next, it goes back to the subclass constructor which initializes the instance variables and executes the instance initializer block in the subclass and finishes off with the rest of the code in the constructor.
The output of the program says it all.
The general procedure, I think is...
As soon as the class is loaded, the static members and static initializer blocks get executed. Then the instance variables are set to their default values. Then the constructor calling starts. (This involves the calling of the superclass's constructor implicitly or explicitly).
Constructor calling can be pretty interesting....
We can envision two scenarios of constructor calling
A. When there is a constructor chaining in the subclass.
1. Here, there is recursive calling of subclass constructors. When the end of the local subclass constructor chaining (using this() call ) is reached, the superclass constructor is invoked explicitly or implicitly.
2. The superclass constructor, for its part, assigns the designated values to the instance variables and then executes the instance initializer blocks and expressions(if any) in the order they are specified.
3. Then, the code in the body of the constructor is executed and the control is transferred to the method that called the constructor. (In the case of the superclass constructor, it is the subclass constructor.)
This routine ensures that the superclass is completely formed even before the subclass constructor has finished executing.
The constructor that receives the control from the superclass constructor is the innermost constructor in the local constructor chain in the subclass. It assigns the designated values to the instance variables and then executes the instance initializer blocks and expressions(if any) in the order they are specified and then relinquishes control to its caller.
That constructor is now run (The proces of initialization carried out only once - in the innermost constructor)and this process is continued until the outermost subclass constructor is executed. The execution of the outermost constructor signals the successful construction of the subclass.
B. When there is no constructor chaining in the subclass.
This is simpler than the case above. Here, as soon as the base class constuctor is called, the superclass constructor is invoked implicitly or explicitly. It assigns the designated value to the instance variables and then executes the instance initializer blocks and expressions(if any) in the order they are provided. The body of the constructor then executes after which control is transferred to the subclass constructor. The subclass constructor repeats what the superclass constructor did and thus constructs the base class.

Hope this helps and do correct me if I am wrong.
Shyam
[This message has been edited by Shyamsundar Gururaj (edited October 22, 2001).]
 
Nain Hwu
Ranch Hand
Posts: 139
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leena,
I suggest you get jdk1.3.1. I know there are
a few bugs in jdk1.2. I don't know if this is one of them.
 
leena rane
Ranch Hand
Posts: 280
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will get it right now Nain
 
Yin Ming
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this topic is described in jls. here is my understanding:
1.initialize the superclass's class.( recursively. same to 2-3 )
2.if the class has not loaded. find it and load it. allocate the enough memory. fill the memory with 0.
so at this time, its class members are defined with default value (binary 0).
3.excute the class initialization. such as static member initialization and static block.
4.initialize the superclass's instance.(recursively. same as 6-7)
5.call the super's constructor. if there is a super(aaa) call in the sub's constructor, it will call the specified constructor. otherwise call the default one. (recursively)
6.initialize the instance. allocate the memory. fill the memory with 0. so the instance members are defined with default value.
7.excute the instance initialization. such as intance member's initialization and the instance initialization block.
8.call the specified constructor.
these codes will helo u:
<code><pre>public class Outer extends Base
{
static
{
System.out.println("Outer static initialization");
}
{
System.out.println("Outer instance initialization");
}
public Outer()
{
System.out.println("Outer construction");
}
public Outer(int i)
{
super(i);
System.out.println("Outer construction with "+i);
}
public static void main(String[] args) {
new Outer();
System.out.println("\n ===== \n");
new Outer(4);
}
}
class Base
{
static
{
System.out.println("base static initialization");
}
{
System.out.println("base instance initialization");
}
public Base()
{
System.out.println("base construction");
}
public Base(int i)
{
System.out.println("base construction with "+i);
}
};</pre></code>
 
Nisheeth Kaushal
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI all,
The output of leena's question is
Main entered
sup
10
11
sup constructor entered
sub
30
31
sub constructor entered
Is it correct according to steps suggested by Yin and others
 
Nisheeth Kaushal
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
According to Gururaj, it will see class Sub after invocation from main method and after that b4 printing "class sub entered" it will call superclass constructor and rather prints "class sup entered" how this logic is possible.
Nisheeth.
 
Nisheeth Kaushal
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Jose i got the point.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic