Friday, June 26, 2009

Nested Classes

When you define a nested class, it is a member of the enclosing class in much the same way as the other class members. A nested class can have an access attribute just like other class members, and the accessibility from outside the enclosing class is determined by the attributes in the same way:

public class Outside {
    // Nested class
    public class Inside {
        // Details of Inside class...
    }
    // More members of Outside class...
}


Static Nested Classes

To make objects of a nested class type independent of objects of the enclosing class type, you can declare the nested class as static. A static nested class can have static members, whereas a non-static nested class cannot.

To demonstrate the use of a nested class I created a partial implementation of a LinkedList, click here to download the code.

No comments:

Post a Comment