Search code examples
javajavacanonymous-class

How are anonymous classes compiled in Java?


I've heard that Java bytecode actually doesn't support any kind of unnamed classes. How does javac translate unnamned classes to named ones?


Solution

  • It synthesizes a name of the form EnclosingClass$n, where "n" is a counter for anonymous classes in EnclosingClass. Because using $ in identifiers is discouraged, these names should not collide with any user-specified names.