Q: SO What Are INNER CLASSESG for Anyway?
A: Believe It or Not, There Are Advantages To Java's Inner Classes. But Before We Go Inta, I'll Provide a Short Background on Inner Classes.
Inner Classes Nest with Inner Classes. A Normal Class Is A Direct Member of a Package, A Top-Level Class. Inner Classes, Which Became Available With Java 1.1, Come In Four Flavors:
Static Member Classes Member Classes Local Classes Anonymous Classes
Let's Take a Quick Look at each in turn.
BRIEFLY, A Static MEMBER CLASS IS A Static Member OF A Class. Like Any Other Static Method, A Static Metr Class Has Access To All Static Methods of The Parent, Or Top-Level, Class.
Like a static member class, a member class is also defined as a member of a class. Unlike the static variety, the member class is instance specific and has access to any and all methods and members, even the parent's this reference.
.
Finally, an Anonymous Class Is a Local Class That Has No Name.
To answer your specific question, I'll focus on the member and anonymous inner classes since those are the ones you'll likely encounter and use To me, the advantages of inner classes can be divided into three categories:. An object-oriented advantage , An Organizational Advantage, And A Call-Back Advantage.
The object-oriented advantage In my humble opinion, the most important feature of the inner class is that it allows you to turn things into objects that you normally would not turn into objects. That allows your code to be even more object-oriented than IT Would Be without Inner Classes.
. Let's look at the member class Since its instance is a member of its parent instance, it has access to every member and method in the parent At first glance, this might not seem like much;. We already have that sort of access from within a method in the parent class. However, the member class allows us to take logic out of the parent and objectify it. For example, a tree class may have a method and many helper methods that perform a search or walk of the tree. From an object-oriented point of view, the tree is a tree, not a search algorithm. However, you need intimate knowledge of the tree's data structures to accomplish a search.An inner class allows us to remove that logic and place it into its own class. So from an object-oriented point of view, we've taken functionality out of where it does not belong and have put it into its own class. Through the use of an inner class, we have successfully decoupled the search algorithm from The Tree. Now, To Change The Search Algorithm, WE CAN S Imply swap in a new class. I Could Go ON, But That Opens Up Our code To Many of The Advantages Provided by Object-Oriented Techniques.
The organizational advantage Object-oriented design is not everyone's thing, but luckily, inner classes provide more. From an organizational point of view, inner classes allow us to further organize our package structure through the use of namespaces. Instead of dumping everything in a FLAT PACKAGE, CLASSES CAN Be Further Nested With Classes. EXPLICITLY, WITHOUT INNER CLASSES, WE WERE LIMITED TO The FOLLOWING HIERARCHY STRUCTURE:
Package1 Class 1 Class 2 ... Class N ... package n
WITH INNER CLASS We can do the folowing:
Package 1 Class 1 Class 2 Class 1 Class 2 ... Class Nused Carefully, Inner Classes Can Provide A Structural Hierarchy That More Naturally Fits your classes.
The callback advantage Inner member classes and anonymous classes both provide a convenient method for defining callbacks. The most obvious example relates to GUI code. However, the application of the callback can extend to many domains.
Most Java GUIs have some kind of component that instigates an actionPerformed () method call. Unfortunately, most developers simply have their main window implement ActionListener. As a result, all components share the same actionPerformed () method. To figure out which component performed the Action, There IS Normal, Ugly Switch In The ActionPerFORMED () Method.
Here's An Example of A Monolithic Implementation:
public class SomeGUI extends JFrame implements ActionListener {protected JButton button1; protected JButton button2; ... protected JButton buttonN; public void actionPerformed (ActionEvent e) {if (e.getSource () == button1) {// do something} else if (E.Getsource () == Button2) {... you get the picture
Whenever you see switches or large if / if else blocks, loud alarm bells should begin to ring in your mind. In general, such constructs are bad object-oriented design since a change in one section of the code may require a corresponding change in the Switch Statement. Inner Member Classes and anonymous classes allow us to get away from the switch ActionPerformed () Method.
Instead, we can define an inner class that implements ActionListener for each component to which we want to listen. That may result in many inner classes. However, we can avoid large switch statements and have the added bonus of encapsulating our action logic. Moreover, . that approach may improve performance in a switch where there are n comparisons, we can expect n / 2 comparisons in the average case Inner classes allow us to set up a 1:.. 1 correspondence between the action performer and the action listener in a . large GUI, such optimizations can make a substantial impact on performance An anonymous approach may look like this: public class SomeGUI extends JFrame {... button member declarations ... protected void buildGUI () {button1 = new JButton (); button2 = New jbutton (); ... button1.addActionListener (new java.awt.event.ActionListener () {public void actionPerformed (java.awt.event.ActionEvent E) {// do som Ehes}}); .. REPEAT for Each Button
Using Inner Member Classes, The Same Program Would Look Like this:
public class SomeGUI extends JFrame {... button member declarations // inner class definitions class Button1Handler implements ActionListener {public void actionPerformed (ActionEvent e) {// do something}} ... define an inner member class for each button protected void buildGUI () {// Initialize the Buttons Button1 = New JButton (); Button2 = New JButton (); ... // register an inner class action listner instance // for each button button1.addactionListener (New Button1Handler ());. Repeat for Each Button
Since inner classes have access to everything in the parent, we can move any logic that would have appeared in a monolithic actionPerformed () implementation to an inner class.I prefer to use member classes as callbacks. However, that is a matter of personal preference I 's I Also Feel That Anonymous Classes Can Become Unwieldyiff Classes Can Become Unwieldy et..
Disadvantages? As with anything else, you have to take the good with the bad. Inner classes have their disadvantages. From a maintenance point of view, inexperienced Java developers may find the inner class difficult to understand. The use of inner classes will also increase the total number of classes in your code. moreover, from a development point of view, most Java tools come up a bit short on their support of inner classes. for example, I use IBM's VisualAge for Java for my day-to-day coding . While inner classes will compile within VisualAge, there is no inner class browser or template. Instead, you must simply type the inner class directly into the class definition. That unfortunately makes browsing the inner class difficult. It is also difficult to type since you Lose Many of Visualage's Code Completion Aids When You Type INTO The Class Definition or Use An Inner Class. Resources
"Inner Classes Specification," from sun, provides an in-depth look at inner classes: http://java.sun.com/ProductS/jdk/1.1/docs/guide/innerclasses/spec/innerclasses.doc.html