13

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

keiter
  • 3,534
  • 28
  • 38

1 Answers1

26

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.

erickson
  • 265,237
  • 58
  • 395
  • 493
  • 5
    `$` is a valid character, but it's not recommended to use it in user-defined names. – axtavt Apr 27 '11 at 18:08
  • 1
    You can easily see this when you compile a class with anonymous inner classes, because you get multiple `*.class` files, with names like `EnclosingClass$n.class`. – Jesper Apr 27 '11 at 18:25
  • Does the counter start from 0 or from 1. And is it continuously numbered from top to bottom of a source file? – johsin18 Mar 07 '14 at 16:15