Search code examples
javagenericsanonymous-classtype-parameter

In Java, can an anonymous class declare its own type parameters?


Can an anonymous class declare its own type parameters?


Solution

  • 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.