Java Reference
In-Depth Information
5.7. Implementation of Nested Types
How the compiler and runtime system deal with nested types would
ideally be transparent to the programmer. Unfortunately, this is not quite
the case. Nested types were added as a language extension, and that
extension had to maintain compatibility with older Java virtual machines.
Consequently, nested types are implemented in terms of a source code
transformation that the compiler applies.
As a programmer, the only thing you should need to know about this
process is the naming conventions that are used. Consider a static, or
non-local, nested type defined as Outer.Inner . This is the source name
for the class. At the virtual machine level the class is renamed as Outer-
$Inner in essence, dots in a nested name are converted to dollar signs.
For local inner classes, the transformation is less specific because these
classes are inaccessible, hence their name is less importantsuch classes
are named Outer$NInner , where Inner is the name of the local class and
N is a sequence of one or more digits. For anonymous inner classes the
names have the form Outer$N , where N is a sequence of one or more di-
gits.
These issues have to be dealt with in two main cases. First, when bund-
ling the class files for your applications you have to recognize all the
strange files with $ in their name. Second, if you use the reflection mech-
anism discussed in Chapter 16 to create nested class instances, you'll
need to know the transformed name. But in most programming the
transformed name is something you can blissfully ignore.
"A power so great, it can only be used for Good or Evil!"
Firesign Theatre, "The Giant Rat of Summatra"
 
Search WWH ::




Custom Search