Java Reference
In-Depth Information
Introducing Nested and Inner Classes
In Java, you can define a nested class . This is a class that is declared within another class.
Frankly, the nested class is a somewhat advanced topic. In fact, nested classes were not
even allowed in the first version of Java. It was not until Java 1.1 that they were added.
However, it is important that you know what they are and the mechanics of how they are
used because they play an important role in many real-world programs.
A nested class does not exist independently of its enclosing class. Thus, the scope of a
nested class is bounded by its outer class. A nested class that is declared directly within
its enclosing class scope is a member of its enclosing class. It is also possible to declare a
nested class that is local to a block.
There are two general types of nested classes: those that are preceded by the static mod-
ifier and those that are not. The only type that we are concerned about in this topic is the
non-static variety. This type of nested class is also called an inner class . It has access to all
of the variables and methods of its outer class and may refer to them directly in the same
way that other non- static members of the outer class do.
Search WWH ::




Custom Search