Nonstatic Member Classes

A nonstatic member class is a non-static member of an enclosing class. Each instance of the nonstatic member class implicitly associates with an instance of the enclosing class. The nonstatic member class's instance methods can call instance methods in the enclosing class and access the enclosing class instance's nonstatic fields.

Listing 4-4 presents a nonstatic member class declaration.

Listing 4-4. Declaring a nonstatic member class public class EnclosingClass {

private int i;

public class EnclosedClass

public void accessEnclosingClass() {

Listing 4-4 declares a top-level class named EnclosingClass with instance field i, instance method ml(), and nonstatic member class EnclosedClass. Furthermore, EnclosedClass declares instance method accessEnclosingClass().

Because accessEnclosingClass() is nonstatic, EnclosedClass must be instantiated before this method can be called. This instantiation must take place via an instance of EnclosingClass. Listing 4-5 accomplishes these tasks.

Listing 4-5. Calling a nonstatic member class's instance method public class NSMCDemo {

public static void main(String[] args) {

EnclosingClass ec = new EnclosingClass(); ec.new EnclosedClass().accessEnclosingClass();

Listing 4-5's main() method first instantiates EnclosingClass and saves its reference in local variable ec. Then, main() uses this reference as a prefix to the new operator, to instantiate EnclosedClass, whose reference is then used to call accessEnclosingClass().

NOTE: Prefixing new with a reference to the enclosing class is rare. Instead, you will typically call an enclosed class's constructor from within a constructor or an instance method of its enclosing class.

Suppose you need to maintain a to-do list of items, where each item consists of a name and a description. After some thought, you create Listing 4-6's ToDo class to implement these items.

Listing 4-6. Implementing to-do items as name-description pairs public class ToDo {

private String name;

private String desc; public ToDo(String name, String desc)

public String getName()

return name;

public String getDesc()

return desc;

public String toString()

return "Name = " + getName() + ", Desc = " + getDesc();

You next create a ToDoList class to store ToDo instances. ToDoList uses its ToDoArray nonstatic member class to store ToDo instances in a growable array—you do not know how many instances will be stored, and Java arrays have fixed lengths. See Listing 4-7.

Listing 4-7. Storing a maximum of two ToDo instances in a ToDoArray instance public class ToDoList {

private ToDoArray toDoArray; private int index = 0; public ToDoList()

toDoArray = new ToDoArray(2);

public boolean hasMoreElements()

return index < toDoArray.size();

public ToDo nextElement()

return toDoArray.get(index++);

public void add(ToDo item)

toDoArray.add(item);

private class ToDoArray private ToDo[] toDoArray; private int index = 0;

ToDoArray(int initSize) {

toDoArray = new ToDo[initSize];

void add(ToDo item) {

ToDo[] temp = new ToDo[toDoArray.length*2]; for (int i = 0; i < toDoArray.length; i++)

return toDoArray[i];

return index;

In addition to providing an add() method to store ToDo instances in the ToDoArray instance, ToDoList provides hasMoreElements() and nextElement() methods to iterate over and return the stored instances. Listing 4-8 demonstrates these methods.

Listing 4-8. Creating a list of ToDo instances and iterating over this list public static void main(String[] args) {

ToDoList toDoList = new ToDoList(); toDoList.add(new ToDo("#1", "Do laundry.")); toDoList.add(new ToDo("#2", "Buy groceries.")); toDoList.add(new ToDo("#3", "Vacuum apartment.")); toDoList.add(new ToDo("#4", "Write report.")); toDoList.add(new ToDo("#5", "Wash car.")); while (toDoList.hasMoreElements())

System.out.println(toDoList.nextElement());

This method generates the following output:

Name = #1, Desc = Do laundry. Name = #2, Desc = Buy groceries. Name = #3, Desc = Vacuum apartment. Name = #4, Desc = Write report. Name = #5, Desc = Wash car.

Java's class library presents many examples of nonstatic member classes. For example, the java.util package's HashMap class declares private Hashlterator, Valuelterator, Keylterator, and Entrylterator classes for iterating over a hashmap's values, keys, and entries. (I will discuss HashMap in Chapter 8.)

NOTE: Code within an enclosed class can obtain a reference to its enclosing class instance by qualifying reserved word this with the enclosing class's name and the member access operator. For example, if code within accessEnclosingClass() needed to obtain a reference to its EnclosingClass instance, it would specify EnclosingClass.this.

Was this article helpful?

0 0

Post a comment