Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

why is it wrong?

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody explain what's wrong here:
1)

2)

Thank you in advance.
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in your first case, u have a blank final class variable.
It must be assigned a value in a static initializer.

the second program is a very good code... where'd u get it from ?
The anonymous sub class being created in testers.add( new tester() { }); must throw an IOException in its constructor, coz its super class, i.e tester throws an IOException.
However, anonymous classes cannot have constructors, therefore my conclusion is :
Its not possible to create an anonymous subclass of a class that throws a checked exception in its constructor which takes no parameters.
More generally, it is not possible to create an anonymous subclass that calls any constructor of its superclass which throws a checked exception.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My comments about your second piece of code -
The language doesn't seem to handle anonymous subclasses of classes whose constructors throw exceptions. While one could argue that perhaps this is a language design error, it is in the language by design.
Note that the other alternative, providing an appropriate throws clause for the default constructor automatically, is precluded by JLS 2e 8.8.7, which specifically states that a default constructor has no throws clause.
Can you try compiling your class with JDK1.3?

Ajith
 
yanish
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deepak, Ajith
thank you for your explanations.
Although I can't find a good reason why the Java Specification imposes such restriction on using of anonimous classes, I take it as it's designed.
But especially I like the compiler's error message:
A default constructor cannot be created for this class because the constructor of its superclass throws the exception java.io.IOException. Define the constructor for this class explicitly.
Unfortunately, I can't test it in jdk1.3
 
reply
    Bookmark Topic Watch Topic
  • New Topic