• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instance initializers

 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Are instance initializers the labeled loops we write? Or are they something else?

Thanks.
 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are instance variables that belong to a class and get initialized before the constructed is called.
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After the constructor is called right?
 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, before the constructor is called.

Suppose the subclass constructor is called which invokes the superclass constructor then, 1st superclass variables are intialized and the constructor is run. Then controls come to subclass constructor where again 1st subclass variables are intialized and then the constructor run.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how about some sample code?
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I correct if I say that in the following code, "initvar" is an instance initializer?

The output of this code is:

In Super's constructor. initvar value = 100
Done calling super() in Sub class


[ September 28, 2008: Message edited by: Rekha Srinath ]
 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Rekha, thats correct.
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic