Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instance initializer

 
Deep Chand
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
This may sound really dumb :-) but I'm confused about 'instance initializer'. I know that static initializer exists for the whole class i.e. run when the class is loaded. Constructor is executed when the instance is created. Then what's this 'instance initializer'?
any comments would be sincerely appreciated.
cheers,
Deep
 
Dhanashree Mankar
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Deep Chand:
Hi all,
This may sound really dumb :-) but I'm confused about 'instance initializer'. I know that static initializer exists for the whole class i.e. run when the class is loaded. Constructor is executed when the instance is created. Then what's this 'instance initializer'?
any comments would be sincerely appreciated.
cheers,
Deep

___________________________________________________________________________
instance initializer is made to use in annonymous classes.
since annonymous classes are not having constructers ,instance initializers are used to initialize variables in annonymous classes.
 
Deep Chand
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does that mean they are only present in the anonymous classes and not anywhere else? Correct?
Thanks,
Deep
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Instance initializer is like static initializer except that the codes within that block are invoked everytime an instance is created.
One of its use is to group codes that are common to all the constructor of the class. Example:
Instead of:

You can change it to:


You can also use it in anonymous class to initialize something in the instance.


Hope this helps.
[ August 26, 2003: Message edited by: Alton Hernandez ]
 
Deep Chand
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic