This is in relation to my answer to a question provided in this thread: Are Inner Classes lightweight?
I remember from my reading that if you can only create one object from a single anonymous inner class, and for this reason, if you want to say create an ActionListener
class and want to create multiple objects from this one class (not using reflection), to not use an anonymous inner class but rather either a private inner class or a stand along class, but folks are telling me I'm wrong. Can someone please clarify this for me? Please check the link as it contains more details, but if anything is unclear, please ask away!
Types of anonymous inner class : Based on declaration and behavior, there are 3 types of anonymous Inner classes: Anonymous Inner class that extends a class : We can have an anonymous inner class that extends a class.For example,we know that we can create a thread by extending a Thread class.
Advantage of Anonymous Inner class in Java. Anonymous Inner class allow you to create more efficient code. They have a concise syntax that reduces clutter in your code. The anonymous inner class has advantage over the inner class in that it closes over the local variables of the method.
Like a nested class, a declaration of a type (such as a variable) in anonymous class shadows any other declarations in the enclosing scope that have the same name. Anonymous classes also have the same restrictions as local classes with respect to their members: We cannot declare static initializers or member interfaces in an anonymous class.
Like local classes, anonymous classes can capture variables; they have the same access to local variables of the enclosing scope: An anonymous class has access to the members of its enclosing class.
You can create any number of anonymous class objects, you can create them in one place in your code (unless you copy the code)
ExecutorService service = ...
for(int i=0;i<1000*1000;i++) {
final int finalI = i;
service.submit(new Runnable() {
public void run() {
System.out.println("Task "+finalI+" run.");
}
});
}
This code will create 1 million objects of the same class.
It is unclear to me. Maybe if we comb through specs we'll find evidence that inner classes should be treated the same as normal classes. However in spirit, an inner class depends on the outer instance, the class doesn't exist beyond the instance. This is different from "normal" classes whose existence is basically perpetual. Two inner classes of two outer instances of course are somewhat related each other, being created by the same source code, yet that doesn't mean they must be identical or even equal.
There are evidence that Java designers intended this way, that an inner class in spirit lives within the scope of the outer instance. For example, the curious syntax outerInstance.new InnerClass()
. For example, no static variables, no static initializers for inner classes. In the discussion of class unloading [1], we see that the argument doesn't really apply to inner classes, it's conceivable that inner classes can be unloaded! It is conceivable that a VM creates a new inner class for each new outer instance.
Practically that's not the case, inner classes are indeed treated the same as normal classes. But conceptually, I'll always think of them differently, as instance-private classes.
[1] http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.7
Update: according to [2]
Two reference types are the same run-time type if They ... have the same binary name
and [3]
The binary name of an anonymous class (§15.9.5) consists of the binary name of its immediately enclosing type, followed by $, followed by a non-empty sequence of digits.
So one anonymous class has one binary name, therefore only one run-time type. The spec guarantees us that different instances of an anonymous class have identitcal class.
[2] http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.3.4
[3] http://java.sun.com/docs/books/jls/third_edition/html/binaryComp.html#44909
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With