This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes which one is executed first Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "which one is executed first" Watch "which one is executed first" New topic
Author

which one is executed first

Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
Hi all,
I do not understand when instance variable gets initialized before constructor or after the constructor
here is the following code:
class Test{
int a=6;
Test(){
this(a);
}
Test(int x){
)
public static void main(String args[]){
Test t=new Test();
}
this code give compiler error saying that a has not been initialized.So i thought that constructors are executed before instance variables.
But this code compiles properly
public class Test{
int i;
Test(){
i++;
}
public static void main(String args[]){
Test t=new Test();
S.o.p(t.i);
}
o/p is 1
Can anyone what's happening in these two codes.
Regards
Neha

nss
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Hi Neha
JLS 8.8.5.1 gives the answer

An explicit constructor invocation statement in a constructor body may not refer to any instance variables or instance methods declared in this class or any superclass, or use this or super in any expression; otherwise, a compile-time error occurs.

HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10


[This message has been edited by Marilyn deQueiroz (edited November 24, 2001).]


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
thanx alot Valentin and Marilyn for your help
Regards
neha
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: which one is executed first
 
Similar Threads
Reference to objects
Question #9 from Doug's book
Regarding Block...
Calling another class
this(i)