aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a question.. 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 "a question.." Watch "a question.." New topic
Author

a question..

Muhammad Shahzad
Ranch Hand

Joined: Feb 17, 2002
Posts: 36
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

Joined: Nov 04, 2001
Posts: 1871
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

Joined: Apr 30, 2003
Posts: 74
Hi Mouhammad
Refer to
JLS 12.5
You will find the exact process that takes place when creating an instance of an object.
Alexan


Alexan Kahkejian<br />SCJP<br />SCWCD<br /><a href="http://www.javaemployer.com" target="_blank" rel="nofollow">http://www.javaemployer.com</a>
Muhammad Shahzad
Ranch Hand

Joined: Feb 17, 2002
Posts: 36
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

Joined: Apr 13, 2003
Posts: 1088
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

Joined: Jul 03, 2001
Posts: 2120
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".


SCJP2. Please Indent your code using UBB Code
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
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

Joined: Feb 17, 2002
Posts: 36
Thanks Alexan, Anupam, Jose for your help.
I'll practice more and more to become the point more clear.
Thanks again.
regards
M.Shahzad
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a question..