<< Chapter < Page Chapter >> Page >

As you can see in Listing 2 , the class named X overrides the toString method to identify itself when called.

(The toString method is automatically called whenever an object's reference is passed as a parameter to the println method.)

The top-level class named A

Listing 3 shows the beginning of the top-level class named A.

Listing 3 shows the declaration of two instance variables and three class variables in the class named A. All of the variables are private, and someare initialized when declared. The three class variables will be used to maintain a count of the number of objects instantiated from the classesnamed A, B, and C.

(Because member classes cannot contain static members , the counter variables for the member classes named B and C were placed in the top-levelclass named A instead of placing them in their respective class definitions.)

Constructor for class A

Listing 4 shows the constructor for the top-level class named A.

Whenever an object of the class named A is instantiated, the constructor does the following:

  • Saves the value of an incoming parameter in a private instance variable named aVar .
  • Increments the object counter named objCntA , maintaining a count of the objects instantiated from class A.
  • Saves the value of the object counter in an instance variable named objNumber to identify the specific object.
  • Displays a message showing the identification of the object being instantiated.

The screen output

The code in Listing 1 instantiates a new object of the class named A, passing the integer value 1 as a parameter to the constructor. As a result, the code inthe constructor shown in Listing 4 produces the screen output shown in Figure 2 .

As you can see from the value of the object counter in Figure 2 , this is the first object instantiated from the class named A.

(The value passed, as a parameter to the constructor, is not displayed by the code in the constructor. That value will be displayed later.)

The method named aShow

The class named A also defines a private method named aShow . I will defer my discussion of that method until later when it is called.

The member class named B

Listing 5 shows the beginning of the member class named B.

If you examine the complete listing of the program in Listing 25 , you will see that the class named B is defined internalto the class named A. In other words, the beginning of the definition of the class named B appears before the curly brace that signals the end of thedefinition of the class named A. Thus, the class named B is a member class of the class named A.

The code in Listing 5 declares two private instance variables and initializes one of them.

Constructor for class B

Listing 6 shows the entire constructor for the class named B.

Whenever an object of the class named B is instantiated, the constructor does the following:

  • Saves the value of an incoming parameter in a private instance variable named bVar .
  • Increments the object counter named objCntB , which is a class variable of the containing top-level class named A, maintaining a count ofobjects instantiated from class B.
  • Saves the value of the object counter in an instance variable named objNumber to identify the specific object.
  • Displays a message showing the identification of the object being instantiated.

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