Inner Member Classes

Java supports an instance class being declared within another class, just as an instance method or instance data field is declared within a class. It is called an inner class, and the class is associated with each instance of the class in which it is nested.

The three varieties of inner class are member class, local class, and anonymous class, each one being a refinement of the one before it. We will look at the special features of each of these kinds of classes and how to use them in the pages ahead. Information on member class is shown in Table 12-2.

Table 12-2. Member class

Java term

Description

Example code

Member class

This is an inner class.

It is a nested class that is not declared “static.” It is an instance member of a class.

class Top {
    class MyMember {
      /* code */
    }
}

The comment saying /* code */ is a reminder that in practice the classes will have bodies with methods and fields.

A member class is like a nested static class without the keyword static. That says, “The class appears in every instance”.

Qualities of an inner member class

Just as any instance member in a class can see all the other members, the scope of an inner class is the entire parent in which it is directly nested.

That is, the inner class can reference any members in its parent. The parent must declare an instance of an inner class before it can invoke the inner class methods, assign to data fields (including private ones), and so on.

The following code shows an example of use.

class top {
     int i=33;

     class myNestedMember { // member inner class
          int k = i;        // note 1 below
          int foo() { return this.k;}  // note 2 below
     }

     void doCalculations () {
          myNestedMember mn1 = new myNestedMember(); //instantiate member
          myNestedMember mn2 = new myNestedMember(); // get another one too
          mn1.k = 564 * mn2.foo();
     }

}

The right way to think of an inner class object is that, because it is associated with an instance of its enclosing class, it has two “this” pointers: One to its own instance data and one to the instance data of its enclosing object. Look at the line marked note 1. The reference to i is a reference to instance data of its enclosing object. Similarly on the line marked note 2, the this.k is a reference to the instance data within the nested class.

Unlike nested static classes, inner classes are not directly part of a package and are not visible outside the class in which they are nested.

Where to use an inner member class

One of the criticisms that people sometimes (rightly) make of OOP is that everything has to be a class. In particular, even if you just want a single function for some utilitarian purpose, you still have to create a class and instantiate an object of that class and invoke a method on that instance. The code for that class might have to be pages and pages away from the place in the source file where you use it. It's a bit heavyweight if your utility function is just a few lines of code. Inner classes are intended to improve the situation.

One of the main uses of inner classes is for GUI event handlers, and we'll see plenty of real examples of them in Chapter 20. They allow you to put the event handling class and method right next to the place where you declare the GUI objects.

Example inner member class from the Java run-time library

The class java.awt.ScrollPane has a member class, as follows:

public class ScrollPane extends Container implements Accessible {
   class PeerFixer implements AdjustmentListener, java.io.Serializable {
       // methods, omitted
   }
   /* more code */
}

The run-time library uses the class PeerFixer which is a member class of ScrollPane to keep a scrolling window in the native GUI aligned with scrolling window object in Java when the user makes adjustments. (The AWT GUI toolkit attempted to use native window system objects with a thin Java layer on top that represents their state to/from Java. This was a clever idea, and achieved a quick'n'dirty implementation of portable windowing. But it proved to be a long-term maintenance nightmare of corner-case bugs where the semantic differences among Windows, Mac, and Unix window systems leaked through).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.235.23