Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is instance variable are initialized before constructor ?

 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have the doubt mentioned in subject line ?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this little 1 minute program:
 
Piyush Sam
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We already had discussion on this topic, plz search for topic before posting new topic


Regards,
Piyush
 
Nischal Tanna
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sakthi Kani
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

click here u will get clear.
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic