This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Instance & Static Initializers Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Instance & Static Initializers" Watch "Instance & Static Initializers" New topic
Author

Instance & Static Initializers

Rishi Raj
Greenhorn

Joined: Nov 12, 2000
Posts: 2
As per some books The instance initializer of a class can throw checked exception withou explicitly handling it .. though in that case all the constructors of the class should include that exception in their 'throws' clause.
Though this seems logical..doesn't seem to work in practice the Java Compiler does not allow instance initializers to throw checked exception ...without the 'try-catch' block.
Can some bosy help me on this.
Thanks
mohit joshi
Ranch Hand

Joined: Sep 23, 2000
Posts: 243
I also tried the same thing and failed. Good that you brought it up.
JLS 8.6. Instance intializers
An instance initializer of a named class may not throw a checked exception unless that exception or one of its superclasses is explicitly declared in the throws clause of each constructor of its class and the class has at least one explicitly declared constructor. An instance initializer in an anonymous class (�15.9.5) can throw any exceptions.
Prasad Ballari
Ranch Hand

Joined: Sep 23, 2000
Posts: 149
Hi,
Thanks for drawing attention for such delicate questions.
regards
Prasad
Aru Ven
Ranch Hand

Joined: Sep 28, 2000
Posts: 199
Thx Rishi & Mohit
For the Question & Answer.
I did'nt know that........ It's a new info to me.
again Thx for sharing....
Aruna
Kartik Shah
Ranch Hand

Joined: Dec 07, 2000
Posts: 102
I am also not able to understand the same thing as specified in JLS.
8.6
"An instance initializer of a named class may not throw a checked exception unless that exception or one of its superclasses is explicitly declared in the throws clause of each constructor of its class and the class has at least one explicitly declared constructor. An instance initializer in an anonymous class (�15.9.5) can throw any exceptions"
But the compiler does not perform the same way ???


Kartik Shah
SCJP, SCDJWS, IBM Certified Websphere & XML, PMP & Six Sigma - http://blog.kartikshah.info
laoniu gougou
Greenhorn

Joined: Jan 25, 2001
Posts: 25
By the way, what is instance initializer? Is it the unnamed block in a class?
Wong KK
Ranch Hand

Joined: Jan 15, 2001
Posts: 52
I've also tried it with code. I need to have try-catch statement if there's check exception possibly be thrown in the instance initializer block.
Are there anyone have ideas on this?
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi All,
See this earlier post http://www.javaranch.com/ubb/Forum24/HTML/007863.html on the same topic.
There is a bug the compiler which causes a problem with instance initializers and exceptions: http://developer.java.sun.com/developer/bugParade/bugs/4409174.html
Hope that helps.

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
laoniu,
Yes, an instance initializer is an unnamed block within a class definition. It is used to initialize the non-static members of each object created from the class, and is called automatically when you create the object.
Static initializer blocks are used to do the same thing for the static members of a class. They look the same, but are preceded with the word <pre><h4>static</h4></pre>.

------------------
~ Ryan Burgdorfer
Java Acolyte in
Columbus, OH USA


<UL TYPE=SQUARE><I><LI>Ryan Burgdorfer<BR><LI>Java Acolyte</I></UL>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Instance & Static Initializers
 
Similar Threads
Anonymous Classes & Exception Handling
Annonymous classes
how do I know a kind of Exception is a Checked Exception?
problem exceptions in intialisers
Exception Handling In Instance Initializer Blocks