Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instance variables & constructors

 
venkatesh badrinathan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try This

 
venkatesh badrinathan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mr.seetharaman.. i have understood now.
 
Saurabh Patil
Ranch Hand
Posts: 48
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Saurabh Patil
Ranch Hand
Posts: 48
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic