File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instance initializer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instance initializer" Watch "instance initializer" New topic
Author

instance initializer

Deep Chand
Ranch Hand

Joined: Dec 17, 2002
Posts: 133
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

Joined: Aug 25, 2003
Posts: 123
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

Joined: Dec 17, 2002
Posts: 133
Does that mean they are only present in the anonymous classes and not anywhere else? Correct?
Thanks,
Deep
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
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

Joined: Dec 17, 2002
Posts: 133
Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instance initializer
 
Similar Threads
How to explain this?
Instance & Static Initializers
New question for tax day
braces without name
static initializer