Can an anonymous class declare its own type parameters?

- 15,750
- 31
- 68
- 83

- 8,227
- 12
- 49
- 68
-
1Could you show some (pseudo)code to clarify your question? – Péter Török Jan 23 '11 at 15:52
-
You could write the code to test this in <30 seconds, and answer your own question. – Matt Ball Jan 23 '11 at 15:53
-
I don't think it's possible, so there's no syntax for it. But, something like: `MyInterface m = new MyInterface()
{ /*body*/ };` – John Assymptoth Jan 23 '11 at 15:54 -
4*@Matt Ball*: I usually do that, but just because you can't find a way to write it, doesn't always mean it's not possible. – John Assymptoth Jan 23 '11 at 15:56
3 Answers
You are right, it's not possible. Since an anonymous class is meant to be used only once, what would be the point of adding type parameters to it which you can never actually use/inherit? You can't instantiate an anonymous class more than once from any other code location than the one which defines it, and you can't subclass it either.

- 114,404
- 31
- 268
- 329
-
1Actually, one can instantiate an anonymous class arbitrarily many times, by executing the class instance creation expression again. – meriton Jan 23 '11 at 16:24
-
1
-
@meriton, good point, I clarified my wording. But still this won't allow us to "reuse" the anonymous class in any way, i.e. refer to it from another code location, which would be required in order to make use of any type parameters. – Péter Török Jan 23 '11 at 16:46
-
2@meriton: What is the class instance creation expression for an anonymous class? How do you execute it a second time? An example would be nice. – Chris Lercher Jan 23 '11 at 16:48
-
@Chris Lercher: for (int i = 0; i < 1000; i++) {list.add(new Object() {});} will create 1000 instances of an anonymous class and add them to a list. – meriton Jan 23 '11 at 21:02
-
No. The Java Language Specification exhaustively defines the possible arguments to a class instance creation expression as follows:
A class instance creation expression specifies a class to be instantiated, possibly followed by type arguments (if the class being instantiated is generic (§8.1.2)), followed by (a possibly empty) list of actual value arguments to the constructor. It is also possible to pass explicit type arguments to the constructor itself (if it is a generic constructor (§8.8.4)). The type arguments to the constructor immediately follow the keyword new. It is a compile-time error if any of the type arguments used in a class instance creation expression are wildcard type arguments (§4.5.1). Class instance creation expressions have two forms:
Unqualified class instance creation expressions begin with the keyword new. An unqualified class instance creation expression may be used to create an instance of a class, regardless of whether the class is a top-level (§7.6), member (§8.5, §9.5), local (§14.3) or anonymous class (§15.9.5).
Qualified class instance creation expressions begin with a Primary. A qualified class instance creation expression enables the creation of instances of inner member classes and their anonymous subclasses.
So while you can specify the actual type parameters of the super class or interface, or the constructor, you can not define new ones. While I grant that this might be useful in some rare cases (because the new type parameter could be used from the class body), there are easy workaround for that:
- wrap the class instance creation expression in a generic method (the anonymous class will see the enclosing method's type parameter)
- use a named class
But, there is a way to use parameters.
Any declared method inside the anonymous class can use the
- properties of the outer class final
- method parameters and final method
- variables
the following code demonstrate it
public class Foo
{
private String value = "Hello ";
public void anonymousTest(final boolean asc)
{
final String world = "world";
new Comparable<String>()
{
@Override
public int compareTo(String o)
{
System.out.println( value + world);
int cmp = value.compareTo(o);
return asc ?cmp :0-cmp;
}
};
}
}
I hope that the example will help.

- 3,452
- 1
- 26
- 23

- 999
- 10
- 18