• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Instance & Static Initializers

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks for drawing attention for such delicate questions.
regards
Prasad
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thx Rishi & Mohit
For the Question & Answer.
I did'nt know that........ It's a new info to me.
again Thx for sharing....
Aruna
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ???

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, what is instance initializer? Is it the unnamed block in a class?
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic