Java Reference
In-Depth Information
A class can also be declared within another class. This type of class is called an inner class. If the class
declared within another class is explicitly or implicitly declared static , it is called a nested class, not an inner
class. The class that contains the inner class is called an enclosing class or an outer class. Consider the following
snippet of code:
package com.jdojo.innerclasses;
public class Outer {
public class Inner {
// Members of the Inner class go here
}
// Other members of the Outer class go here
}
The Outer class is a top-level class. It is a member of the com.jdojo.innerclasses package. The Inner class is
an inner class. It is a member of the Outer class. The Outer class is the enclosing (or outer) class for the Inner class.
An inner class can be the enclosing class for another inner class. There are no limits on the levels of nesting of inner
classes.
An instance of an inner class can only exist within an instance of its enclosing class. That is, you must have an
instance of the enclosing class before you can create an instance of an inner class. This is useful in enforcing the rule
that one object cannot exist without the other. For example, a computer must exist before a processor can exist; an
organization must exist before a president for that organization exists. In such cases, Processor and President can be
defined as inner classes whereas Computer and Organization are their enclosing classes, respectively. An inner class
has full access to all the members, including private members, of its enclosing class.
Java 1.0 did not support inner classes. They were added to Java 1.1 without any changes in the way the JVM used
to handle the class files. How was it possible to add a new construct like an inner class without affecting the JVM?
Inner classes have been implemented fully with the help of the compiler. The compiler generates a separate class file
for each inner class in the compilation unit. The class files for inner classes have the same format as the class files for
the top-level classes. Therefore, the JVM treats the class files for an inner and top-level classes the same. However, the
compiler has to do a lot of behind-the-scenes work to implement inner classes. I will discuss some of the work done
by the compiler to implement inner classes.
You may ask whether it is possible to achieve everything in Java that is facilitated by inner classes without using
them. To some extent, the answer is yes. You can implement most of the functionalities, if not all, provided by inner
classes without using the inner classes. The compiler generates additional code for an inner class. Instead of using
inner class constructs and letting the compiler generate the additional code for you, you can write the same code
yourself. This idea sounds easy. However, who wants to reinvent the wheel?
Advantages of Using Inner Classes
The following are some of the advantages of inner classes. Subsequent sections in this chapter explain all of the
advantages of inner classes with examples.
They let you define classes near other classes that will use them. For example, a computer
will use a processor, so it is better to define a Processor class as an inner class of the
Computer class.
They provide an additional namespace to manage class structures. For example, before
the introduction of inner classes, a class can only be a member of a package. With the
introduction of inner classes, top-level classes, which can contain inner classes, provide an
additional namespace.
 
Search WWH ::




Custom Search