Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a question..

 
Muhammad Shahzad
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
According to my knowledge, the sequence of a programme is :
static variables are initialized..
static blocks are executed..
instance variables are initialized.
instance blocks are executed.
then Constructors() are executed.
This question is from Khalid and Rolf's book.
Please guide me what is happening here."According to my knowledge."
Thanks.
public class MyClass {
public static void main(String args[]){
MyClass obj= new MyClass(l);
}

static int i=5;
static int l;
int j=7;
int k;

public MyClass(int m){
System.out.println(i + ", "+ j + " ,"+ k + " ,"+ l + ", "+ m);
}

{ j=70;l=20;}
static {i=50;}

}
//50, 70, 0, 20, 0
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
the sequence is little different,
1. static vars
2. static blocks
3. super constructors (in case of the super classes) (also, once super constructor is called this same seqence of operations would occur for that super class you know...so its like go up , up , up....come down...down...)
4. instance members
5. instance blocks
6. the constructor
try the follwoing code and see what u get. comapre it with ur predictions before u ran the code.

i've not compiled this code. so please make changes if u run into compilation errors. i'm sorry for that but my machine where i'm typing this message is having 24MB RAM so i can't think of running jdk here.
regards
maulin
 
Alexan Kahkejian
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mouhammad
Refer to
JLS 12.5
You will find the exact process that takes place when creating an instance of an object.
Alexan
 
Muhammad Shahzad
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Maulin,
I have made the code compileable.
But i think i need more explanation with more simple code.
Thansk in anticipation.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the sequence is
1. static variables.
2. static blocks.
3. constructor of the class whose main() method has been called which then calls the superclass constructors and if the superclass also a superclass its constructor is run and so on until the top most constructor starts running. Then the top most constructor returns and then the constructors under it.
3. instance members.
4. Now its the turn of instance blocks.
Third last and Second last steps(all numbered 3) will happen according as to where the steps have been placed. That is a step which is reached first will be executed first(amongst these two).

for example see the code below

[ May 17, 2003: Message edited by: Anupam Sinha ]
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

1. static vars
2. static blocks
.
4. instance members
5. instance blocks

The order of static vars and static blocks is not fixed, but the one they appear in the source.
Similarly, instance members and instance blocks are executed "in order of appearance".
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Jose. But according to me the member block will be executed when the constructor has begun executing and before the constructor finishes it will go through all the blocks of code in order of appearance. With blocks of the superclass being executed first.
I hope this will help.
filename : test.java
 
Muhammad Shahzad
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Alexan, Anupam, Jose for your help.
I'll practice more and more to become the point more clear.
Thanks again.
regards
M.Shahzad
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic