Contents | Prev | Next Inner Classes Specification

How does the Java Language Specification change for inner classes?

There are few significant changes, since the new features primarily relax restrictions in the existing language, and work out new implications for the old design. The key change is that types can now have types as members. (But type names can't contain instance expressions.) The basic definitions of scope, name scoping, member naming, and member access control are unchanged.

Here are the extensions to the class body and block syntax:

        ClassMemberDeclaration, InterfaceMemberDeclaration:


A type which is a type member is inherited by subtypes, and may be hidden in them by type declarations of the same name. (Types are never "virtual.") Members which are types may be declared private or protected.

A non-static member class, or a class defined by a block or expression, is an inner class. All other classes are top-level. Inner classes may not declare static members, static initializers, or member interfaces. Package members are never static. But a class which is a member of a top-level class may be declared static, thereby declaring it also to be a top-level class. Interfaces are always static, as are their non-method members.

A class may not have the same simple name as any of its enclosing classes.

The keyword this can be qualified, to select one of possibly several current instances. (Inner classes have two or more current instances.)

. this

The syntax for class instance creation extended to support anonymous classes and enclosing instances:

        new TypeName ( ArgumentListopt ) ClassBodyopt
        Primary . new Identifier ( ArgumentListopt ) ClassBodyopt

A new expression may define an anonymous class by specifying its body. Independently, the type of a new expression may specified as the simple name of an inner class, if an instance of the immediately enclosing class is given as a qualifying expression before the keyword new. The qualifying instance becomes the enclosing instance of the new object. A corresponding qualification of super allows a subclass constructor to specify an enclosing instance for a superclass which is an inner:

        ExplicitConstructorInvocation: ...
         Primary . super ( ArgumentListOpt ) ;

If an inner class is constructed by an unqualified new or super expression, the enclosing instance will be the (innermost) current instance of the required type.

Some of the detailed descriptions of name binding in the 1.0 Java Language Specification require amendment to reflect the new regularity in lexical scoping. For example, a simple variable name refers to the innermost lexically apparent definition, whether that definition comes from a class or a block. The same is true for simple type names. The grammar for a qualifier name (i.e., an AmbiguousName) is extended to reflect the possibility of class names qualifying other type names. The initial simple name in a qualified type name is taken to be a class name if a class of that name is in scope; otherwise it is taken to be a package name, as in Java 1.0.

Any inherited member m of a subclass C is in scope within the body of C, including any inner classes within C. If C itself is an inner class, there may be definitions of the same kind (variable, method, or type) for m in enclosing scopes. (The scopes may be blocks, classes, or packages.) In all such cases, the inherited member m hides the other definitions of m. Additionally, unless the hidden definition is a package member, the simple name m is illegal; the programmer must write C.this.m.

Nested classes of all sorts (top-level or inner) can be imported by either kind of import statement. Class names in import statements must be fully package qualified, and be resolvable without reference to inheritance relations. As in Java 1.0, it is illegal for a class and a package of the same name to co-exist.

A break or continue statement must refer to a label within the immediately enclosing method or initializer block. There are no non-local jumps.

The checking of definite assignment includes classes defined by blocks and expressions, and extends to occurrences of variables within those classes. Any local variable used but not defined in an inner class must be declared final, and must be definitely assigned before the body of the inner class.

Contents | Prev | Next

Inner Classes Specification (HTML generated by dkramer on March 15, 1997)
Copyright © 1996, 1997 Sun Microsystems, Inc. All rights reserved
Please send any comments or corrections to