Download the JDK
Search the Tutorials
Hide the TOC
Trail: Learning the Java Language
Lesson: Classes and Objects
Classes and Objects
Classes
Declaring Classes
Declaring Member Variables
Defining Methods
Providing Constructors for Your Classes
Passing Information to a Method or a Constructor
Objects
Creating Objects
Using Objects
More on Classes
Returning a Value from a Method
Using the this Keyword
Controlling Access to Members of a Class
Understanding Instance and Class Members
Initializing Fields
Summary of Creating and Using Classes and Objects
Questions and Exercises
Questions and Exercises
Nested Classes
Inner Class Example
Summary of Nested Classes
Questions and Exercises
Enum Types
Questions and Exercises
Annotations
Questions and Exercises
Home Page > Learning the Java Language > Classes and Objects
Nested Classes

> The Java programming language allows you to define a class within another class. Such a class is called a nested class and is illustrated here:

> >     class OuterClass {
> >         ...
> >         class NestedClass {
> >             ...
> >         }
> >     }

> A nested class is a member of its enclosing class and, as such, has access to other members of the enclosing class, even if they are declared private. As a member of `OuterClass`, a nested class can be declared `private`, `public`, `protected`, or package private. (Recall that outer classes can only be declared `public` or package private.)

> > Terminology: Nested classes are divided into two categories: static and non-static. Nested classes that are declared `static` are simply called static nested classes. Non-static nested classes are called inner classes.

> >     class OuterClass {
> >         ...
> >         static class StaticNestedClass {
> >             ...
> >         }
> >         class InnerClass {
> >             ...
> >         }
> >     }

> ### Why Use Nested Classes?

> There are several compelling reasons for using nested classes, among them:
> - It is a way of logically grouping classes that are only used in one place.
> - It increases encapsulation.
> - Nested classes can lead to more readable and maintainable code.

> Logical grouping of classes—If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.

> Increased encapsulation—Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared `private`. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.

> More readable, maintainable code—Nesting small classes within top-level classes places the code closer to where it is used.

> ### Static Nested Classes

> As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class — it can use them only through an object reference.

> > Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.

> Static nested classes are accessed using the enclosing class name:

> >     OuterClass.StaticNestedClass

> For example, to create an object for the static nested class, use this syntax:

> >     OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();

> ### Inner Classes

> As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.

> Objects that are instances of an inner class exist within an instance of the outer class. Consider the following classes:

> >     class OuterClass {
> >         ...
> >         class InnerClass {
> >             ...
> >         }
> >     }

> An instance of `InnerClass` can exist only within an instance of `OuterClass` and has direct access to the methods and fields of its enclosing instance. The next figure illustrates this idea.

> An InnerClass Exists Within an Instance of OuterClass

> To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:

> >     OuterClass.InnerClass innerObject = outerObject.new InnerClass();

> Additionally, there are two special kinds of inner classes: local classes and anonymous classes (also called anonymous inner classes). Both of these will be discussed briefly in the next section.

Previous page: Questions and Exercises: Objects
Next page: Inner Class Example