<< Chapter < Page Chapter >> Page >

The private showA method

Listing 5 shows a private method named showA . This method displays the following information about an object instantiated from class A :

  • The relative time that the object was instantiated.
  • The value of the instance variable named xVar , which is an instance variable of the superclass of class A .
  • The name of the class file that represents the class named A .

Code in methods belonging to an object of the local class named B , contained in an object of class A , has direct access to private members of objects of the containing or enclosing class.

The private showA method will be called by a method named showB , belonging to an object of the local class B , to demonstrate the truth of this statement.

The method named delay

Listing 6 shows a utility method named delay , which is an instance method of class A .

The purpose of the delay method is simply to insert a 30-millisecond delay in the execution of the program. This method is used to cause certainactivities in the execution of the program to be spread far enough apart in time to make them distinguishable, one from the other, on the basis of relative time.

The method named meth

The method named meth is an instance method of the class A . The beginning of this method is shown in Listing 7 .

The method named meth contains a local class definition for a class named B , which we will examine later.

As you can see in Listing 7 , it also declares a final local variable named methTime . As you will see later, the method named meth instantiates two separate objects of local class B and calls a method named showB on each of those objects.

The method named showB displays various data values that illustrate the characteristics of local classes, including the value of the final local variable named methTime .

Access to final local variables

One of the characteristics of a local class is that objects of a local class have access to local variables within the same scope, provided thatthose local variables are declared final .

Blank final variables

The code in Listing 7 declares a final local variable named methTime . Because of the syntax used, this variable is known as a blank final variable. A blank final variable is not initialized when it is declared.

As with all local variables, the variable cannot be used until a value has been assigned to it. Because this variable is declared final , once a value has been assigned to it, thevalue cannot be changed throughout the remaining life of the variable.

Set value of blank final variable

The code in Listing 8

  • Inserts a delay
  • Sets a value for the final local variable named methTime
  • Displays the value that was set in methTime
  • Inserts an additional delay before continuing

The value that is set in the variable named methTime is the relative time that the statement is executed.

The screen output

The code in the main method of Listing 1 calls meth on a new object of class A , producing the screen output shown in Figure 3 .

(Once again, the actual time value displayed by your system may be different, depending on the speed of your system and other factors aswell.)

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  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.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask