A look at inner classes
Reduce class clutter in your Java designs: Use inner classes
JavaWorld.com, 10/01/97
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
In my December 1996 column titled "
Generically speaking," I discussed the difficulty of building a generic collection class in Java. Since that time two things have happened: Sun
created
inner classes to assist in the implementation of Java Beans; and Sun has proposed a basic
collection class for Java. I like Sun's proposal, but I wish the company had included a way of doing
type enforcement. For those of you not familiar with the term, type enforcement is a mechanism that ensures that objects placed into a collection
are of the proper type. Currently, the collections use class
Object as a generic reference which, if through a bug in your class, you store an unexpected type into the collection, will cause
a class cast exception when you retrieve the stored reference from the collection. For an example of a class that provides
type enforcement, see the class
ContainerOrganizer in the
Resources section below.
If you think that type enforcement in collections is a Good Thing, I encourage you to write Sun at collection-comments.
For this column I thought it would be useful to revisit the class we designed last December and replace that example's "mixin"
classes with inner classes.
What are inner classes?
Inner classes are class files whose "name" is scoped to be inside another class. What do I mean by "scope"? The scope of a
name defines when that name can be referenced by Java code. When two classes share the same package, they are said to have
"package scope." Classes that are outside the containing package cannot refer to classes inside the package that are not declared
as being public. Thus the names of the classes are scoped to the package.
Inner classes are scoped to the class used to declare them -- thus they are effectively "invisible" to the other classes in
the same package. This lack of visibility to other classes in the package gives the programmer the opportunity to create a
set of classes within a containing class without cluttering up the name space of classes in their package.
According to Sun's design documents, the Sun engineers felt that inner classes removed what had been a wart in the definition
of Java. The wart was that a Java class could only be defined as a peer of other classes. This created an artificial level
that applied only to class definitions. Inner classes primarily are a change to Java compilers that gives them a mechanism
to eliminate this wart. To support backwards compatibility with existing virtual machines, the inner classes are actually
compiled to regular class files, only the names are changed to prevent them from colliding with existing classes. For more
information on this "exchange of warts," I refer you to the complete design document.
The changes that were made to the Java language that would allow the language to support inner classes consisted of changing
the rules about where you could declare a class. If you have been programming in Java 1.0x, you know that you can declare
multiple classes in a single source file (as long as only one of them is public) and that the declaration of the second and
subsequent classes must follow the closing brace of the first class. That is now changed in 1.1: Now you can declare a class
within another class. The following example will illustrate this more clearly. In Java 1.0 you had to declare two classes in a file
sequentially as shown in the code below.
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- Previous "Java In Depth" articles
- "Take an in-depth look at the Java Reflection API"
Learn about the new Java 1.1 tools for finding out information about classes. http://www.javaworld.com/javaworld/jw-09-1997/jw-09-indepth.html
- "Take a look inside Java classes"
Learn to deduce properties of a Java class from inside a Java program. http://www.javaworld.com/javaworld/jw-08-1997/jw-08-indepth.html
- "Build an interpreter in Java -- Implement the execution engine"
Here's how to take the interpreter classes and run with them. http://www.javaworld.com/javaworld/jw-07-1997/jw-07-indepth.html
- "How to build an interpreter in Java, Part 2The structure"
The trick to assembling the foundation classes for a simple interpreter. http://www.javaworld.com/javaworld/jw-06-1997/jw-06-indepth.html
- "How to build an interpreter in Java, Part 1The BASICs"
For complex applications requiring a scripting language, Java can be used to implement the interpreter, adding scripting
abilities to any Java app. http://www.javaworld.com/javaworld/jw-05-1997/jw-05-indepth.html
- "Lexical analysis, Part 2Build an application"
How to use the StreamTokenizer object to implement an interactive calculator. http://www.javaworld.com/javaworld/jw-02-1997/jw-02-indepth.html
- "Lexical analysis and JavaPart 1"
Learn how to convert human-readable text into machine-readable data using the StringTokenizer and StreamTokenizer classes.
http://www.javaworld.com/javaworld/jw-01-1997/jw-01-indepth.html
- "Code reuse and object-oriented systems"
Use a helper class to enforce dynamic behavior. http://www.javaworld.com/javaworld/jw-12-1996/jw-12-indepth.html
- "Container support for objects in Java 1.0.2"
Organizing objects is easy when you put them into containers. This article walks you through the design and implementation
of a container. http://www.javaworld.com/javaworld/jw-11-1996/jw-11-indepth.html
- "The basics of Java class loaders"
The fundamentals of this key component of the Java architecture. http://www.javaworld.com/javaworld/jw-10-1996/jw-10-indepth.html
- "Not using garbage collection"
Minimize heap thrashing in your Java programs. http://www.javaworld.com/javaworld/jw-09-1996/jw-09-indepth.html
- "Threads and applets and visual controls"
This final part of the series explores reading multiple data channels. http://www.javaworld.com/javaworld/jw-07-1996/jw-07-mcmanis.html
- "Using communication channels in applets, Part 3"
Develop Visual Basic-style techniques to applet design -- and convert temperatures in the process. http://www.javaworld.com/javaworld/jw-06-1996/jw-06-mcmanis.html
- "Synchronizing threads in Java, Part II"
Learn how to write a data channel class, and then create a simple example application that illustrates a real-world implementation
of the class. http://www.javaworld.com/javaworld/jw-05-1996/jw-05-mcmanis.html
- "Synchronizing threads in Java"
Former Java team developer Chuck McManis walks you through a simple example illustrating how to synchronize threads to assure
reliable and predictable applet behavior. http://www.javaworld.com/javaworld/jw-04-1996/jw-04-synch.html