Java Reference
In-Depth Information
Inner classes are invisible to all other classes, which means that you don't have to
worry about name conflicts between it and other classes.
n
Inner classes can have access to variables and methods within the scope of a top-
level class that they would not have as a separate class.
n
In many cases, an inner class is a short class file that exists only for a limited purpose. In
the SquareTool application, because the Square class doesn't contain a lot of complex
behavior and attributes, it is well suited for implementation as an inner class.
The name of an inner class is associated with the name of the class in which it is con-
tained, and it is assigned automatically when the program is compiled. The Square class
is given the name SquareTool$Square.class by the Java compiler.
CAUTION
When using inner classes, you must be even more careful to
include all .class files when making a program available. Each
inner class has its own class file, and these class files must be
included along with any top-level classes.
Inner classes, although seemingly a minor enhancement, actually represent a significant
modification to the language.
Rules governing the scope of an inner class closely match those governing variables. An
inner class's name is not visible outside its scope, except in a fully qualified name, which
helps in structuring classes within a package. The code for an inner class can use simple
names from enclosing scopes, including class and member variables of enclosing classes,
as well as local variables of enclosing blocks.
In addition, you can define a top-level class as a static member of another top-level class.
Unlike an inner class, a top-level class cannot directly use the instance variables of any
other class. The ability to nest classes in this way allows any top-level class to provide a
package-style organization for a logically related group of secondary top-level classes.
6
Summary
Today, you learned how to encapsulate an object by using access control modifiers for its
variables and methods. You also learned how to use other modifiers such as static ,
final , and abstract in the development of Java classes and class hierarchies.
To further the effort of developing a set of classes and using them, you learned how to
group classes into packages. These groupings better organize your programs and enable
the sharing of classes with the many other Java programmers making their code publicly
available.
 
Search WWH ::




Custom Search