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 why is it wrong? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why is it wrong?" Watch "why is it wrong?" New topic
Author

why is it wrong?

yanish
Greenhorn

Joined: Aug 18, 2000
Posts: 21
Can anybody explain what's wrong here:
1)

2)

Thank you in advance.
Deepak M
Ranch Hand

Joined: Jul 10, 2000
Posts: 124
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.
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
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


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
yanish
Greenhorn

Joined: Aug 18, 2000
Posts: 21
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why is it wrong?