next up previous contents
Next: Primitive Types Up: Other Topics Previous: Class Initialization   Contents

Inner and Anonymous Classes

Inner and anonymous classes are actually compiled to regular classes by a Java compiler following some special naming convention. For example, an inner class Inner defined in Outer is accessed via the dot operator in a program, Outer.Inner but the external name of the class once it is compiled is Outer$Inner. Thus, one can get a Class class for these types of classes using the standard Class.forName().

However, anonymous classes can not be instantiated [JPL pg. 287] and inner class constructors are always transformed to take a parameter that is a reference to the enclosing object, so Class::newInstance() cannot be used because no inner class will have a no-argument constructor. Instead, one must obtain a Constructor object from the inner class Class and invoke it with the appropriate arguments.



Nadeem Hamid 2000-07-24