<< Chapter < Page | Chapter >> Page > |
Accessing private members
In those cases where it is necessary for an object of the local class to access private members of the containing object, the compiler automaticallycreates and uses accessor methods that make such access possible.
Similar to your code
The bottom line is that the code that is automatically produced by the compiler is probably very similar to code that you would write if you werewriting the program using only top-level classes. The good news is that you don't have to write that extra code, and you don't have to maintain it. Theextra code is written for you, and if you modify your class structure, the extra code is automatically modified accordingly.
The paragraphs that follow will explain a program named InnerClasses07 . This program is designed specifically to illustrate various characteristics of localclasses. I will discuss the program in fragments. A complete listing is shown in Listing 17 near the end of the module.
This program illustrates the use of local classes. The program consists of a total of five classes:
When compiled, the program produces the class files shown in Figure 1 . The file named A$1$B.class represents the local class. The remaining files in Figure 1 represent the four top-level classes.
In the previous module, I explained that once you understand the class file naming convention, you can determine from the filenames how top-level classes and member classes are structured. However, the situation isn't nearly so clear when it comes to local classes and anonymousclasses. This will become more apparent in the next module, which combines local classes and anonymous classes.
Containment hierarchy
The program named InnerClasses07 defines a local class named B inside an instance method named meth .
The method named meth is an instance method of a top-level class named A . The method named meth instantiates two separate objects of the local class named B , and calls a method named showB on each of them.
The method named showB displays certain data values that illustrate the characteristics of localclasses, as well as the containment hierarchy among objects of the local class and an object of the containing class.
In this case, objects of the class named B are contained within an object of the class named A . (The class named A is an enclosing class of the class named B .)
Inheritance hierarchy
The top-level class named A extends the top-level class named X , (which in turn, extends Object ).
The class named B is contained in or enclosed by the top-level class named A , but extends the top-level class named Y , (which in turn, extends Object ).
There is no inheritance relationship between the classes X and Y (aside from their common superclass named Object ).
There is no inheritance relationship between the classes A and B .
The method named showB also displays data values that are intended to demonstrate that the inheritance hierarchy is independent of thecontainment hierarchy.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?