Java Reference
In-Depth Information
TIP: (continued)
As another example, if you only need one object of a class and the class definition is
very short, many programmers like to use an anonymous class (but I must admit I am
not one of them).
When we study linked lists in Chapter 15, you will see cases where using an inner
class as a helping class makes the linked list class self-contained in a very natural way.
We will also use inner classes when defining Graphical User Interfaces (GUIs) starting
in Chapter 17. But until you learn what linked lists and GUIs are, these are not likely
to be compelling examples.
Self-Test Exercise
28. Suppose we replace
NumberCarrier anObject
with
Object anObject
in Display 13.11. What would be the first statement in the program to cause an
error message? Would it be a compiler error message or a run-time error message?
Chapter Summary
An interface is a property of a class that says what methods a class that implements
the interface must have.
An interface is defined the same way as a class is defined except that the keyword
interface is used in place of the keyword class and method bodies are replaced
by semicolons.
An interface may not have any instance variables, with one exception: An inter-
face may have defined constants. If you use the syntax for an instance variable in
an inner class, the variable is automatically a constant, not a real instance variable.
An inner class is a class defined within another class.
One simple use of an inner class is as a helping class to be used in the definition of
the outer class methods and/or instance variables.
A static inner class is one that is not associated with an object of the outer class. It
must include the modifier static in its class heading.
To create an object of a nonstatic inner class outside the definition of the outer
class, you must first create an object of the outer class and use it to create an object
of the inner class.
Search WWH ::




Custom Search