Free Guides

Language Tutorials

                            

SUN

Your Ad Here

 
               SUN CERTIFIED JAVA PROGRAMMER FOR JAVA2 PLATFORM 1.4

       Index     

Nested classes and Interfaces

Entity

Declaration

Context

Accessib-ility Modifiers

Enclosing Instance

Direct Access to Enclosing Context

Declarations in Entity Body

Top-Level Class (or Interface)

Package

Public or default

No

N/A

All that are valid in a class (or interface) body

Static Member Class (or Interface)12

As static member of enclosing class or interface

All

No

Static members in enclosing context

All that are valid in a class (or interface) body

Non-static Member Class13

As non-static member of enclosing class or interface

All

Yes

All members in enclosing context

Only non-static declarations + final static fields

Local Class14

In block with non-static context

None

Yes

All members in enclosing context + final local variables

Only non-static declarations + final static fields

In block with static context

None

No

Static members in enclosing context + final local variables

Only non-static declarations + final static fields

Anonymous Class

As expression in non-static context

None

Yes

All members in enclosing context + final local variables

Only non-static declarations + final static fields

As expression in static context

None

No

Static members in enclosing context + final local variables

Only non-static declarations + final static fields

  • class TLC { //Top Level Class
    static class SMC {} //Static Member Class
    interface SMI {} //Static Member Interface
    class NSMC {} //Non-static Member Class
    void nsm() {
    class NSLC {} //Non-static Local Class
    }
    static void sm() {
    class SLC {} //Static Local Class
    }
    SMC nsf = new SMC () {}; //Anonymous non-static
    static SMI sf = new SMI() {}; //Anonymous Static
    }

  • The expression
    <enclosing class name>.this
    evaluates to a reference that denotes the enclosing object (ie. of class
    <enclosing class name>) of current instance of non-static member class.

  • In the Anonymous class, SMC is not the name of the Anonymous class but the name of the class that you are extending or the interface you are implementing.