Forums Register Login

Anonymous classes

+Pie Number of slices to send: Send
Hi,
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
+Pie Number of slices to send: Send
Howdy!
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
Cheers,
Kathy
+Pie Number of slices to send: Send
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
Hoo hoo hoo! Looks like we got a live one! Here, wave this tiny ad at it:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 843 times.
Similar Threads
Regarding Anonymous and Static classes
Using A-D.A.I.C.
interface implementation and inner classes
What is use of Local inner classes?
Anonymous classes
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:52:15.