018 Nested-Classes
018 Nested-Classes
Learn the structure, syntax, and programming paradigm of the Java platform and language.
Start by mastering the essentials of object-oriented programming on the Java platform, and
progress incrementally to the more-sophisticated syntax and libraries that you need to develop
complex, real-world Java applications.
Unit objectives
• Know how to define nested classes and when it's appropriate to use them
• Understand the side effects of using nested classes
• Comprehend the special use of new operator with nested classes
• Know how and when to use static inner classes and anonymous inner classes
}
}
Like member variables and methods, Java classes can also be defined at any scope including
public, private, or protected. Nested classes can be useful when you want to handle internal
processing within your class in an object-oriented fashion but limit the functionality to the class
where you need it.
Typically, you use a nested class when you need a class that's tightly coupled with the class in
which it's defined. A nested class has access to the private data within its enclosing class, but this
structure carries with it side effects that aren't obvious when you start working with nested classes.
Suppose you have the following relationship between a Manager and a nested class called
DirectReports, which is a collection of the Employees that report to that Manager:
Just as each Manager object represents a unique human being, the DirectReports object
represents a collection of actual people (employees) who report to a manager. DirectReports
differ from one Manager to another. In this case, it makes sense that I would only reference the
DirectReports nested class in the context of its enclosing instance of Manager, so I've made it
private.
The code in Listing 1 doesn't work, and you're probably wondering why. The problem (and also its
solution) lies with the way DirectReports is defined within Manager, and with the rules of scope.
To create an instance of a public nested class, you use a special version of the new operator.
Combined with a reference to an enclosing instance of an outer class, new gives you a way you to
create an instance of the nested class:
public class Manager extends Employee {
public Manager() {
}
. . .
public class DirectReports {
. . .
}
}
// Meanwhile, in another method somewhere...
public static void main(String[] args) {
Manager manager = new Manager();
Manager.DirectReports dr = manager.new DirectReports();
}
Note on line 12 that the syntax calls for a reference to the enclosing instance, plus a dot and the
new keyword, followed by the class you want to create.
In this case, you don't need an enclosing instance. Static inner classes act like their regular Java
class counterparts, and you should use them only when you need to couple a class tightly with its
definition. Clearly, in the case of a utility class like ManagerComparator, creating an external class
is unnecessary and potentially clutters up your code base. Defining such classes as static inner
classes is the way to go.
Listing 2 builds on the example in Listing 1 in Unit 17: Interfaces, adding a default method for
handling Employee types that are not StockOptionEligible. The listing starts with a method in
HumanResourcesApplication to process the stock options, followed by a JUnit test to drive the
method.
}
}
In the Listing 2 example, I provide implementations of two interfaces that use anonymous
inner classes. First are two separate implementations of StockOptionEligible— one for
Employees and one for Persons (to obey the interface). Then comes an implementation of
StockOptionProcessingCallback that's used to handle processing of stock options for the Manager
instances.
It might take time to grasp the concept of anonymous inner classes, but they're super handy. I use
them all the time in my Java code. And as you progress as a Java developer, I believe you will too.
import java.util.logging.Logger;
return inner;
}
import java.util.logging.Logger;
interface HelloCallback {
void sayHello(String whatToSay);
}
}
6. Is it possible to write an inner class that can be instantiated by any class in your application
(regardless of what package in which it resides) without an enclosing instance of the outer
class? Explain your answer.
7. What's the difference between a nested class an an inner class?
a. An inner class is one that is defined using private access only, whereas a nested class is
declared static.
Previous: Interfaces
Next: Regular expressions