Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes is instance variable are initialized before constructor ? 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 "is instance variable are initialized before constructor ?" Watch "is instance variable are initialized before constructor ?" New topic
Author

is instance variable are initialized before constructor ?

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
i have the doubt mentioned in subject line ?


Thanks and Regards, Amit Taneja
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Try this little 1 minute program:


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Piyush Sam
Greenhorn

Joined: Jun 02, 2005
Posts: 24
We already had discussion on this topic, plz search for topic before posting new topic


Regards,
Piyush


Piyush Jain<br /> <br />Being happy doesn't mean everything's perfect. It means you've decided to see beyond the imperfections.
Nischal Tanna
Ranch Hand

Joined: Aug 19, 2003
Posts: 182
Originally posted by Piyush Sam:
We already had discussion on this topic, plz search for topic before posting new topic


Regards,
Piyush


the Answer to this question would be Yes/No/Dont know... Please dont mis lead the person asking questions by behaving as Bar Tenders!!

Coming back to the Q, yes the instance variables are initialised before the constructor being called as the instance variables may be used in the constructor. Hope this helps


Thnx
Sakthi Kani
Ranch Hand

Joined: Mar 29, 2005
Posts: 98
Hi

click here u will get clear.


" Don't be afraid of pressure. Remember that pressure is what turns a lump of coal into a diamond... " <br /> <br />Thanks & Regards...<br />Sakthi<br />SCJP1.4, OCA
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
Please dont mis lead the person asking questions by behaving as Bar Tenders!!


Yet you feel the need to do exactly the same thing???

The reasons behind not crossposting questions to multiple forums (fora?) are also valid for multiple postings of the same question. There are many times where a question is asked, and a search in the same forum reveals upward of 100 hits! There is no reason to start a new thread for a question which has been answered dozens of times.

I also feel that the criticism of people "behaving as bartenders" is unfounded and a bit hypocritical when made by a non-bartender.
soumya ravindranath
Ranch Hand

Joined: Jan 26, 2001
Posts: 300
I believe it's not only a Bartender's job to keep the forum fit. The poster (?! I mean the one who posts) also has a choice to say something to remarks such as "please search" and explain at least once why he/she needs to post repeatedly.
Repeated posts ( and posts with incomprehensible titles ) cause much suffering to those who use the search facility extensively.
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
public class ZZZ{
int i;

{
i++;
System.out.println("Instance init:" + i);
i++;
}
i = 42;
ZZZ() {
System.out.println("ZZZ(): " + i);
}


public static void main(String[] args) {
new ZZZ(); } }


why this is giving error like
D:\java_prac>java ZZZ
Instance init:1
ZZZ(): 2

and can anybody pls tell the rule of initializing and some cautions/tips while dealing with this question

one small wisdom i found is that "Instance intializer will always run before the class constructor even initializer is after the constructor, provided it can access the variable it accessing."

other wisdome is "java don't initialize its instance variables to value defined untill and unless all the super constructor finished and completed"

any other such tips will be easily remembered

sorry if this thread is reapeting..kindly continue..
try to make it unique by giving such valuable java wisdoms.

regards
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
from B Eckel's Thinking in Java:
Within a class, the order of initialization is determined by the order that the variables are defined within the class. The variable definitions may be scattered throughout and in between method definitions, but the variables are initialized before any methods can be called´┐Żeven the constructor.


java amateur
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: is instance variable are initialized before constructor ?