<< Chapter < Page Chapter >> Page >

Call the private bShow method

The code in Listing 21 is somewhat more interesting. This code calls the private bShow method on the separate object instantiated from the class named B in order to identify the object to which that object is linked.

The screen output

The output produced by the code in Listing 21 is shown in Figure 14 . Even though this object of the member class B was instantiated from within the constructor for the member class named C, theobject of the class named B is internally linked to the object of the class named A that was originally used to instantiate the object of the class named C.

(See Listing 1 where the objects of classes named A, B, and C were originally instantiated. This object of the class named B is a differentobject from the object of the class named B instantiated in Listing 1 . This object of the class named B was instantiated by the code in Listing 11 .)

How is this determined from Figure 14 ?

The second line in Figure 14 shows that a parameter value of 20 was received by the constructor when theobject of the class named B was instantiated. This corresponds to the instantiation of the object by the code in the constructor in Listing 11 .

The third line in Figure 14 shows that this was the second object instantiated from the class named B. (See the definition of the bShow method in Listing 12 , which displays the value stored in a variable that is used to save the object number.)

The proof of the pudding

Now recall that the method named bShow (belonging to an object of the class B) calls the method named aShow belonging to the object of the class named A to which it is internally linked.

The fourth line in Figure 14 shows the value of the parameter passed to the constructor for the object of class A when that object was instantiated. (See the definition of the aShow method in Listing 13 .) This value corresponds to the value that was passed to the constructor for the original object of class A whenit was constructed in Listing 1 .

(It does not correspond to the value passed to the constructor for the class named A when the object of the class A was constructed in Listing 11 .)

The fifth line in Figure 14 shows that the object was the first object instantiated from the class named A.

Both B objects link to the same A object

Thus, both objects instantiated from the class named B in this program are internally linked to the same object instantiated from the classnamed A, which is the enclosing class for the class named B.

(However, had I instantiated the new object of the class B using a statement such as the following,

new A(100).new B(200).bShow();

the new object of the class B would have been linked to the new object of theclass A rather than being linked to the original object of the class A. As you can see, keeping mental track of which object is linked to which otherobject could become complicated.)

Call the aShow method on the other A object

The code in Listing 22 calls the private aShow method on the independent object of the class A that was instantiated in the constructor for class C, shown in Listing 11 .

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