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 Anonymous inner class .... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Anonymous inner class ...." Watch "Anonymous inner class ...." New topic
Author

Anonymous inner class ....

Shrini Kulkarni
Ranch Hand

Joined: Jan 12, 2001
Posts: 63
Hi ,
Look at this piece of code :
class superclass
{
superclass()
{
};
void aMethod()
{
}
}
public class ee
{
//following commented method does not compile
// gives error : type expected on the line .. new superclass ..
/*
public void useAnonymousInnerclass(
new superclass()
{
public SomeMethod()
{
System.out.println ("anonymous inner class ");
}
}
);
*/

// where as this method compiles..
public superclass AnotherWayOfUsingAnoInnerClass()
{
return new superclass()
{
int ii =0;
};
}
}
Any clues as why this is happening ?
Shrinivas

Sun Certified programmer for Java2 platform.<BR>Shri_mk@hotmail.com
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi,
In your first method (the one that won't compile) you are trying to accept an anonymous class as an input parameter to a method. If the class is anonymous how on earth will you ever be able to call the method?
In your second method you are correctly returning an anonymous class so the caller can then make use of it.
Regards,
Manfred.
Shrini Kulkarni
Ranch Hand

Joined: Jan 12, 2001
Posts: 63
OK got the point :
We can use the anonyumous classes in a method call where an object reference is expected Not while declaring method itself.
Quote from KAM :
"An anonymous class can be used where a reference can be used"
Thanks for clarification
Shrinivas
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Anonymous inner class ....
 
Similar Threads
Is there a way to instantiate anonymous inner class?
How to access an Inner Class' method
How can anonymous class extend any other class?
does anoymous inner class have a constructor?
Anonymous Inner Class Question