Complete Program Listing

A complete listing of the program discussed in this lesson is show in Listing 18 below.

Listing 18. Complete program listing.
/*File InnerClasses07.java
Copyright 2003 R.G.Baldwin

Illustrates the use of local classes.

This program defines a local class named B inside
an instance method named meth.  The method named
meth is an instance method of a class named A.

The method named meth instantiates two separate
objects of the local class named B, and invokes
on each of them a method named showB, which
displays certain data values that illustrate the
characteristics of local classes, a well as the
relationships among objects of a local class and
an object to which that object is internally
associated.  In this case, objects of the class
named B are internally associated with an object
of the class named A.

This program produces the following class files
when compiled:

A$1$B.class
A.class
InnerClasses07.class
X.class
Y.class

The file named A$1$B.class represents the local
class named B.

This program produces the following output:

In xstr for A, aTime = 10
In meth, methTime = 40
----------------------
Instantiate first B-object
In xstr for B, bTime = 70
Delay and instantiate second B-object
In xstr for B, bTime = 100
----------------------
Display first B-Object
-1-
In showB, private bTime = 70
In showB, private aTime = 10
In showB, final methTime = 40
In showB, protected xVar = 1000
In showB, protected yVar = 2000
In showB, class name = A$1$B
-2-
In showA, aTime = 10
In showA, xVar = 1000
In showA, class name = A
-3-
In showB, bTime = 70
In showB, aTime = 10
-4-
toString in class B, bTime = 70
toString in class Y, yVar = 2000
----------------------
Display second B-Object
-1-
In showB, private bTime = 100
In showB, private aTime = 10
In showB, final methTime = 40
In showB, protected xVar = 1000
In showB, protected yVar = 2000
In showB, class name = A$1$B
-2-
In showA, aTime = 10
In showA, xVar = 1000
In showA, class name = A
-3-
In showB, bTime = 100
In showB, aTime = 10
-4-
toString in class B, bTime = 100
toString in class Y, yVar = 2000


Tested using SDK 1.4.1 under WinXP
************************************************/
import java.util.Date;

public class InnerClasses07{
  //The following static variable is used as a
  // base to establish the relative points in
  // time that certain activities occur during
  // the execution of the program.
  static long baseTime = new Date().getTime();

  public static void main(String[] args){
    //Instantiate a new object of the class named
    // A and invoke the method named meth on that
    // object.  This method will sequentially
    // instantiate two separate objects of a
    // local class named B that is defined inside
    // the method named meth.  Then it will
    // invoke a method named showB on each of
    // those objects to cause them to display
    // various data values that illustrate the
    // characteristics of local classes.
    new A().meth();
  }//end main
}//end class InnerClasses07
//=============================================//

//The class named A extends this class to
// illustrate the difference between the
// inheritance hierarchy and the containment
// hierarchy.
class X{
  protected int xVar = 1000;
}//end class X
//=============================================//

//The local class named B extends this class to
// illustrate the difference between the
// inheritance hierarchy and the containment
// hierarchy.
class Y{
  protected int yVar = 2000;

  //Overridden toString method
  public String toString(){
    return "toString in class Y, yVar = " + yVar;
  }//end overridden toString
}//end class Y
//=============================================//

class A extends X{
  //Establish the relative time that the object
  // of class A is instantiated.
  private long aTime = new Date().getTime() -
                         InnerClasses07.baseTime;

  A(){//constructor
    System.out.println(
              "In xstr for A, aTime = " + aTime);
  }//end constructor
  //-------------------------------------------//

  //Displays information about the object
  // instantiated from class A.
  private void showA(){
    System.out.println(
                   "In showA, aTime = " + aTime);
    System.out.println(
                     "In showA, xVar = " + xVar);
    System.out.println("In showA, class name = "
                         + getClass().getName());
  }//end showA
  //-------------------------------------------//

  //Method used to insert a time delay of 30
  // milliseconds.
  void delay(){
    try{
      Thread.currentThread().sleep(30);
    }catch(InterruptedException e){
      System.out.println(e);
    }//end catch
  }//end delay
  //-------------------------------------------//

  //This method contains a local class definition
  // for a class named B.  The method
  // instantiates two separate objects of class B
  // and invokes a method named showB on each of
  // the objects.  The method named showB
  // displays various data values that illustrate
  // the characteristics of local classes.
  void meth(){
    //The following local variable must be final
    // to be accessible from a local class.  This
    // is a blank final variable whose value can
    // be set once and never changed after that.
    final long methTime;

    //Delay and then set the value of the blank
    // final local variable to a relative time.
    // Then delay again before continuing.
    delay();
    methTime = new Date().getTime() -
                         InnerClasses07.baseTime;
    System.out.println(
              "In meth, methTime = " + methTime);
    delay();
  //-------------------------------------------//

    //This is the definition of a local class
    // named B.  Note that as with local
    // variables, the class definition must
    // appear before the code that attempts to
    // instantiate the class.  The class extends
    // class Y to illustrate that the inheritance
    // hierarchy is independent of the
    // containment hierarchy.
    class B extends Y{
      private long bTime;

      B(){//constructor
        //Establish the relative time that the
        // object is instantiated.
        bTime = new Date().getTime() -
                         InnerClasses07.baseTime;
        System.out.println(
              "In xstr for B, bTime = " + bTime);
      }//end constructor

      void showB(){
        //Display private and protected
        // variables, some of which belong to the
        // internally associated object
        // instantiated from the class named A.
        // Note that code in this method has
        // access to xVar, which is a protected
        // member variable of a superclass of the
        // class named A.
        System.out.println("-1-");
        System.out.println(
           "In showB, private bTime = " + bTime);
        System.out.println(
           "In showB, private aTime = " + aTime);
        System.out.println(
                  "In showB, final methTime = " +
                                       methTime);
        System.out.println(
           "In showB, protected xVar = " + xVar);
        System.out.println(
           "In showB, protected yVar = " + yVar);
        System.out.println(
                      "In showB, class name = " +
                           getClass().getName());

        System.out.println("-2-");
        //Invoke the private method named showA
        // in the internally associated object
        // instantiated from the class named A,
        // to illustrate the containment
        // hierarchy.
        showA();

        System.out.println("-3-");
        //Show how to access individual objects,
        // including the internally associated
        // object instantiated from the class
        // named A.
        System.out.println(
              "In showB, bTime = " + this.bTime);
        System.out.println(
            "In showB, aTime = " + A.this.aTime);

        System.out.println("-4-");
        //Illustrate the inheritance hierarchy by
        // invoking the overridden toString
        // methods belonging to the class named B
        // and its superclass named Y.
        System.out.println(toString());
        System.out.println(super.toString());
      }//end showB
      //---------------------------------------//

      //Overridden toString method
      public String toString(){
        return
         "toString in class B, bTime = " + bTime;
      }//end overridden toString
    }//end local class B

    //This is the code that is executed when this
    // method named meth is invoked.  Instantiate
    // two objects from the class named B and
    // invoke the method named showB on each of
    // them.  Those methods will, in turn invoke
    // the method named showA on the internally
    // associated object instantiated from the
    // class named A.  Insert a delay between the
    // instantiation of the two objects.
    System.out.println("----------------------");
    System.out.println(
                   "Instantiate first B-object");
    final B obj1 = new B();
    System.out.println(
        "Delay and instantiate second B-object");
    delay();
    final B obj2 = new B();

    System.out.println("----------------------");
    System.out.println("Display first B-Object");
    obj1.showB();
    System.out.println("----------------------");
    System.out.println(
                      "Display second B-Object");
    obj2.showB();

  }// end meth
}//end class A
File: eb.htm [Next] [Prev]