Nested Top-Level Versus Member Classes


[Page 832 (continued)]

The Converter class (Fig. F.1) shows the differences between a nested top-level class and a member class. The program is a somewhat contrived example that performs various kinds of metric conversions. The outer Converter class serves as a container for the inner classes, Distance and Weight, which perform specific conversions.


[Page 833]
Figure F.1. A Java application containing a top-level nested class.

public class Converter {    private static final double INCH PER METER = 39.37;    private final double LBS PER KG = 2.2;    public static class Distance {                    // Nested Top-level class      public double metersToInches(double meters) {        return meters * INCH PER METER;      }    } // Distance class    public class Weight {                             // Member class      public double kgsToPounds(double kg) {        return kg * LBS PER KG;      }    } // Weight class } // Converter class public class ConverterUser {    public static void main(String args[]) {      Converter.Distance distance = new Converter.Distance();      Converter converter = new Converter();      Converter.Weight weight = converter.new Weight();      System.out.println( "5 m = " + distance.metersToInches(5) + " in");      System.out.println( "5 kg = " + weight.kgsToPounds(5) + " lbs");    } // main() } // ConverterUser class 

The Distance class is declared static, so it is a top-level class. It is contained in the Converter class. Note the syntax used in ConverterUser.main() to create an instance of the Distance class:

Converter.Distance distance = new Converter.Distance(); 


A fully qualified name is used to refer to the static inner class via its containing class.

The Weight class is not declared static. It is, therefore, associated with instances of the Converter class. Note the syntax used to create an instance of the Weight class:

Converter converter = new Converter(); Converter.Weight weight = converter.new Weight(); 


Before you can create an instance of Weight, you have to declare an instance of Converter. In this example, we have used two statements to create the weight object, which requires using the temporary variable, converter, as a reference to the Converter object. We could also have done this with a single statement by using the following syntax:

Converter.Weight weight = new Converter().new Weight(); 



[Page 834]

In either case, the qualified name Converter.Weight must be used to access the inner class from the ConverterUser class.

There are a couple of other noteworthy features in this example. First, an inner top-level class is really just a programming convenience. It behaves just like any other top-level class in Java. One restriction on top-level inner classes is that they can only be contained within other top-level classes, although they can be nested one within the other. For example, we could nest additional converter classes within the Distance class. Java provides special syntax for referring to such nested classes.

Unlike a top-level class, a member class is nested within an instance of its containing class. Because of this, it can refer to instance variables (LBS_PER_KG) and instance methods of its containing class, even to those declared private. By contrast, a top-level inner class can only refer to class variables (INCH_PER_METER)that is, to variables declared static. So you would use a member class if it were necessary to refer to instances of the containing class.

There are many other subtle points associated with member classes, including special language syntax that can be used to refer to nested member classes and rules that govern inheritance and scope of member classes. For these details, consult the Java Language Specification, which can be accessed online at

http://java.sun.com/docs/books/jls/html/index.html




Java, Java, Java(c) Object-Orienting Problem Solving
Java, Java, Java, Object-Oriented Problem Solving (3rd Edition)
ISBN: 0131474340
EAN: 2147483647
Year: 2005
Pages: 275

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net