aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anonymous classes 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 "Anonymous classes" Watch "Anonymous classes" New topic

Anonymous classes

Bhaskara Reddy

Joined: Jan 20, 2003
Posts: 1
Iam very much confused with anonymous classes. Can any one give a detailed explanation of what an anonymous class consists and what it does.
Thanks for your time and help
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
An anonymous class is a special type of inner/nested class, that is used when you need a "just-in-time" class. There are religious wars over whether anonymous inner classes are a good thing, but I won't talk about that
An anonymous class can be defined inside a method or as part of an instance variable assignment. It can even be defined inside a method *argument*. The syntax looks really strange...

This line:

Might appear to be creating a new instance of JPanel, but look carefully... it's creating a SUBCLASS of JPanel. If it were creating a new instance, it would say:
JPanel p = new JPanel(); <-- note the semicolon
But because the new JPanel() is followed with an opening curly brace instead of a semicolon, that tells the compiler, "Create me a new instance of a new SUBCLASS of JPanel. The new subclass does not have a name (well, it does, but the compiler chooses it), and here is the definition of the class (that includes the method I overrided from JPanel, the paintComponent method)
So if the compiler sees a curly brace instead of semicolon after the new SomeClassName() it tells the compiler to make an anonymous subclass, and then make an instance of that subclass, and assign that new instance-of-the-new-anonymous-subclass to the declared variable. The joy of polymorphism allows you to assign a subclass object to a superclass references, so it's perfectly OK.
The next one is a bit more interesting:
Runnable r = new Runnable() {
public void run() { }
Because NOW it looks like you are trying to instantiate an interface! Runnable is not a class, so you are never allowed to say:
Runnable r = new Runnable();
BUT... you ARE allowed to say to the compiler, "Please make me a new class (do not care about the name of the class) that IMPLEMENTS the Runnable interface, and then make me an instance of that class and assign it to the variable of type Runnable."
Bottom line:
If the compiler sees that curly brace { instead of a semicolon after the new <class or interface name> it knows you want an anonymous class. That anonymous class can be one of two things --
1) If the thing after the new is a CLASS name, the anonymous class is a SUBCLASS of that named type
The JPanel is an example of making a subclass.
2) If the thing after the new is an INTERFACE name, the anonymous class is a CLASS THAT IMPLEMENTS the named interface type.

So why would you do it? Look at the example inside the main method. You're going along and you need something to run in a Thread but OH NO! You do not have a Runnable class (in other words, a class that implements Runnable) with the run() method you need. No problem -- you just make one right there. Just in time.
Again, I will resist telling you what I REALLY think of this practice.
However, you can definitely expect to see a lot of this code on the exam, including within questions that are not about anonymous classes. They are often used in examples simply to make the code smaller.
Or, to make it more confusing
Sarma Lolla
Ranch Hand

Joined: Oct 21, 2002
Posts: 203
No one can beat Kathy in explaining complicated things like inner classes. How ever I just want to add the following basic points from Exam point of view.
a) Anonymous inner classes can extend at most one class or implement one interface only.
b) These can't have any constructors.
c) They can only access the final variables declared in the method or the final parameters that are sent to the method in which the inner class is created. The anonymous class can also access the all the member variables of the enclosing class
I agree. Here's the link:
subject: Anonymous classes
Similar Threads
Anonymous classes
Regarding Anonymous and Static classes
interface implementation and inner classes
Using A-D.A.I.C.
What is use of Local inner classes?