This page is optimized for mobile devices, if you would prefer the desktop version just click here

7.2 Java1636 java oop member classes  (Page 3/15)

What does Flanagan have to say?

Here is how David Flanagan, author of Java in a Nutshell, summarizes his discussion of member classes.

"A class defined as a member (non-static) of another. Each instance has an enclosing instance, and can use its members. New syntax for this , new , and super . Cannot have static members. Cannot have same name as containing class."

According to Flanagan, the main features of member classes are:

  • Every instance of a member class is internally associated with an instance of the class that defines or contains the member class.
  • The methods of a member class can implicitly refer to the fields defined within the member class, as well as those defined by any enclosing class,including private fields of the enclosing class.

Smoke and mirrors

Every class definition in a Java program, including nested top-level classes, member classes, local classes, and anonymous classes, produces a class file whenthe program is compiled. According to Flanagan,

"The Java Virtual Machine knows nothing about nested top-level classes and interfaces or the various types of inner classes. Therefore, the Javacompiler must convert these new types into standard non-nested class files that the Java interpreter can understand. This is done through source codetransformations that insert $ characters into nested class names. These source code transformations may also insert hidden fields, methods, andconstructor arguments into the affected classes."

A reference to the containing object

For example, the compiler automatically inserts a private instance variable in the member class to hold a reference to the containing object. It alsoinserts a hidden argument in all constructors for the member class, and passes the containing object's reference to the constructor for the member class. Themodified constructor saves that reference in the private instance variable of the object of the member class. Thus each object instantiated from the memberclass contains a private reference to the containing object.

Accessing private members

In those cases where it is necessary for an object of the member 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 of 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.

Discussion and sample code

The paragraphs that follow will explain a program named InnerClasses06 , which is designed specifically to illustrate various characteristics of memberclasses. I will discuss the program in fragments. A complete listing is shown in Listing 25 near the end of the module.

<< Chapter < Page Page > Chapter >>

Read also:

OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.