<< Chapter < Page | Chapter >> Page > |
Listing 5 - The showA method in 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 |
Listing 6 - The delay method in class A. |
---|
void delay(){
try{Thread.currentThread().sleep(30);
}catch(InterruptedException e){System.out.println(e);
}//end catch}//end delay |
Listing 7 - Beginning the meth method in class A. |
---|
void meth(){
final long methTime; |
Listing 8 - Set value of blank final variable. |
---|
delay();
methTime = new Date().getTime() -InnerClasses07.baseTime;
System.out.println("In meth, methTime = " + methTime);
delay(); |
Listing 9 - Beginning of class definition for local class B. |
---|
class B extends Y{
private long bTime;B(){//constructor
bTime = new Date().getTime() -InnerClasses07.baseTime;
System.out.println("In xstr for B, bTime = " + bTime);
}//end constructor |
Listing 10 - Instantiate two objects of local class B. |
---|
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(); |
Listing 11 - Call showB on the first object. |
---|
System.out.println("----------------------");
System.out.println("Display first B-Object");obj1.showB() |
Listing 12 - Beginning of showB method in local class B. |
---|
//Continuing with local class B definition
void showB(){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()); |
Listing 13 - More code from showB method. |
---|
System.out.println("-2-");
showA(); |
Listing 14 - Use of the keyword this. |
---|
System.out.println("-3-");
System.out.println("In showB, bTime = " + this.bTime);
System.out.println("In showB, aTime = " + A.this.aTime) |
Listing 15 - Illustrate the inheritance hierarchy. |
---|
System.out.println("-4-");
System.out.println(toString());System.out.println(super.toString());
}//end showB//---------------------------------------//
//Overridden toString methodpublic String toString(){
return"toString in class B, bTime = " + bTime;
}//end overridden toString}//end local class B |
Listing 16 - Call showB method on second object. |
---|
System.out.println("----------------------");
System.out.println("Display second B-Object");
obj2.showB();}// end meth
}//end class A |
Listing 17 - Complete program listing.
/*File InnerClasses07.java
Copyright 2003 R.G.BaldwinIllustrates the use of local classes.
This program defines a local class named B insidean 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 callson each of them a method named showB, which
displays certain data values that illustrate thecharacteristics of local classes, a well as the
relationships among objects of a local class andan object to which that object is internally
associated. In this case, objects of the classnamed 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.classInnerClasses07.class
X.classY.class
The file named A$1$B.class represents the localclass named B.
This program produces the following output:In xstr for A, aTime = 10
In meth, methTime = 40----------------------
Instantiate first B-objectIn xstr for B, bTime = 70
Delay and instantiate second B-objectIn xstr for B, bTime = 100
----------------------Display first B-Object
-1-In showB, private bTime = 70
In showB, private aTime = 10In showB, final methTime = 40
In showB, protected xVar = 1000In showB, protected yVar = 2000
In showB, class name = A$1$B-2-
In showA, aTime = 10In showA, xVar = 1000
In showA, class name = A-3-
In showB, bTime = 70In 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 = 100In showB, private aTime = 10
In showB, final methTime = 40In showB, protected xVar = 1000
In showB, protected yVar = 2000In showB, class name = A$1$B
-2-In showA, aTime = 10
In showA, xVar = 1000In showA, class name = A
-3-In showB, bTime = 100
In showB, aTime = 10-4-
toString in class B, bTime = 100toString 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 call 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// call 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 methodpublic 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 calls 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-");//Call 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
// calling 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 called. Instantiate// two objects from the class named B and
// call the method named showB on each of// them. Those methods will, in turn call
// 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//=============================================//
-end-
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?