This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instance variables & constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instance variables & constructors" Watch "instance variables & constructors" New topic
Author

instance variables & constructors

venkatesh badrinathan
Ranch Hand

Joined: Aug 03, 2008
Posts: 77
hi guys.. i have read from kathy book that 'INSTANCE MEMBERS ARE ACCESSIBLE ONLY AFTER THE SUPER CONSTRUCTOR RUNS', i have tried a lot to understand what that have meant, but could not understand.. can anybody help me please..


SCJP1.5
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Try This

venkatesh badrinathan
Ranch Hand

Joined: Aug 03, 2008
Posts: 77
Thanks Mr.seetharaman.. i have understood now.
Saurabh Patil
Ranch Hand

Joined: Dec 11, 2005
Posts: 45

I think he is not getting the concept itself, the code can make him understand better..but we need some theory to get what is actually going on... so here it is...

Instance members are associated to the object rather than the class. So to access an instance member you first have to tell JVM which object's instance memeber it is and for that the object needs to be in existance ( i.e. in memory). Whenever you create an object say like this..

Box b = new Box();

then unless the constructor ( and the constructors of all super classes class Box inherits from) completes the object doesnt come to life...its yet to take birth. Hence before the constructors complete their job you don't even have the object, then how can you access its member? You can't kiss the baby before its born!

I hope you understand now.. to confuse you more ( actually to make you understand in better way) you can call the static members of the class from constructors... now start thinking why... you will understand the concept.

Now please execute the code my friend gave above..


All for one and one for all!
Saurabh Patil
Ranch Hand

Joined: Dec 11, 2005
Posts: 45

Originally posted by venkatesh badrinathan:
Thanks Mr.seetharaman.. i have understood now.



oopss.. venkatesh..my post was just 7 minutes after yours..so i didnt know you got it when I was posting it..
anyways..its good you got it.
venkatesh badrinathan
Ranch Hand

Joined: Aug 03, 2008
Posts: 77
thanks Mr.Saurabh Patil, your explanation was excellent, i am now clear about the concept that i could explain to anyone and also, accessing static members from constructors is possible because we dont need an object to access static members, hence we do. i think i am right...???
Saurabh Patil
Ranch Hand

Joined: Dec 11, 2005
Posts: 45

Originally posted by venkatesh badrinathan:
thanks Mr.Saurabh Patil, your explanation was excellent, i am now clear about the concept that i could explain to anyone and also, accessing static members from constructors is possible because we dont need an object to access static members, hence we do. i think i am right...???


Yes, you are right.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instance variables & constructors