Chapter 5.8 – Nested Classes | Introduction to Programming Using Java

Chapter 5.8 – Nested Classes | Introduction to Programming Using Java

 

5.8 Nested Classes

 

java 3 Chapter 5.8 - Nested Classes | Introduction to Programming Using Java

 

A class seems like it should be a pretty important thing. A class is a high-level building block of a program, representing a potentially complex idea and its associated data and behaviors. I’ve always felt a bit silly writing tiny little classes that exist only to group a few scraps of data together. However, such trivial classes are often useful and even essential. Fortunately, in Java, I can ease the embarrassment, because one class can be nested inside another class.

My trivial little class doesn’t have to stand on its own. It becomes part of a larger more respectable class. This is particularly useful when you want to create a little class specifically to support the work of a larger class. And, more seriously, there are other good reasons for nesting the definition of one class inside another class.

In Java, a nested class is any class whose definition is inside the definition of another class. (In fact, a class can even be nested inside a subroutine, which must, of course, itself be inside a class). Nested classes can be either named or anonymous. I will come back to the topic of anonymous classes later in this section. A named nested class, like most other things that occur in classes, can be either static or non-static.

 

Chapter 5.8 - Nested Classes | Introduction to Programming Using Java

 

5.8.1  Static Nested Classes

 

The definition of a static nested class looks just like the definition of any other class, except that it is nested inside another class and it has the modifier static as part of its declaration. A static nested class is part of the static structure of the containing class. It can be used inside that class to create objects in the usual way. If it is used outside the containing class, its name must indicate its membership in the containing class.

 

nested classes

 

That is, the full name of the static nested class consists of the name of the class in which it is nested, followed by a period, followed by the name of the nested class. This is similar to other static components of a class: A static nested class is part of the class itself in the same way that static member variables are parts of the class itself.

For example, suppose a class named WireFrameModel represents a set of lines in threedimensional space. (Such models are used to represent three-dimensional objects in graphics programs.) Suppose that the WireFrameModel class contains a static nested class, Line, that represents a single line.

Then, outside of the class WireFrameModel, the Line class would be referred to as WireFrameModel.Line. Of course, this just follows the normal naming convention for static members of a class. The definition of the WireFrameModel class with its nested Line class would look, in outline, like this:

public class WireFrameModel {

 

Chapter 5.8 - Nested Classes | Introduction to Programming Using Java

 

The full name of the nested class is WireFrameModel.Line. That name can be used, for example, to declare variables. Inside the WireFrameModel class, a Line object would be created with the constructor “new Line()”. Outside the class, “new WireFrameModel.Line()” would be used.

A static nested class has full access to the static members of the containing class, even to the private members. Similarly, the containing class has full access to the members of the nested class, even if they are marked private. This can be another motivation for declaring a nested class, since it lets you give one class access to the private members of another class without making those members generally available to other classes. Note also that a nested class can itself be private, meaning that it can only be used inside the class in which it is nested.

When you compile the above class definition, two class files will be created. Even though the definition of Line is nested inside WireFrameModel, the compiled Line class is stored in a separate file. The name of the class file for Line will be WireFrameModel$Line.class.

 

 

 

Read More…

Introduction to Programming Using Java – David J. Eck

Chapter 5.2 – Constructors and Object Initialization | Introduction to Programming Using Java

Chapter 5.2.2 – Constructors | Introduction to Programming Using Java

Chapter 5.5.3 – Example: Vehicles | Introduction to Programming Using Java

Chapter 5.6 – This and Super | Introduction to Programming Using Java

Chapter 5.7 – Interfaces | Introduction to Programming Using Java

24 thoughts on “Chapter 5.8 – Nested Classes | Introduction to Programming Using Java”

Leave a Comment