<< Chapter < Page Chapter >> Page >

Save the reference to the array object

The reference to the array object is stored in the local reference variable named objRef of type Object . We know that the reference is assignment compatible with this reference variable because the Object type is completely generic. All non-primitive types are assignment compatiblewith type Object .

The class named Prob05MyClassA

At this point, I am going to put the explanation of the class named Prob05 temporarily on hold and explain the class named Prob05MyClassA , which is shown in its entirety in Listing 2 .

Listing 2 . The class named Prob05MyClassA.
class Prob05MyClassA extends Prob05{ private int data;public Prob05MyClassA(int inData){ System.out.println("Prob05");System.out.println("Dick"); data = inData;}//end constructor public int getData(){return data; }//end getData()}//end class Prob05MyClassA

Note that the class named Prob05MyClassA extends the class named Prob05 , which is partially shown in Listing 1 .

Familiar code

All of the code in Listing 2 should be familiar to you because it is very similar to the code in the previous module. Therefore, noexplanation of Listing 2 is warranted.

Save the incoming value

In summary, when the object of type Prob05MyClassA is instantiated, it saves the value of an incoming constructor parameter in a private instancevariable.

Return the saved value

When the method named getData is called on a reference to the object, it returns a copy of that value.

A review

To review what I have already said, the array object that was instantiated in Listing 1 contains a reference to this object of type Prob05MyClassA in the only element of the one-element array.

The reference to the array object is stored in the reference variable named objRef .

Indirection at work

At this point, objRef contains a reference to an array object, one element of which contains a reference to an ordinary object, which is locatedsomewhere else in memory. This is indirection.

The next statement in the main method

Returning now to the main method that began in Listing 1 , Listing 3 shows the next statement in the main method following the last statement in Listing 1 .

Listing 3 . The next statement in the main method.
System.out.println( new Prob05MyClassB().getDataFromObj(objRef[0]));

What is an anonymous object?

An anonymous object is an object whose reference is not saved in a named reference variable.

Instantiate an anonymous object

Consider the parameter list of the println method shown in Listing 3 . A new object of the Prob05MyClassB class is instantiated in the parameter list. However, the reference to that object is not saved in a named referencevariable. Instead, that reference is used to immediately call the method named getDataFromObj that belongs to the anonymous object.

The parameter that is passed...

Now consider the parameter that is passed to the method named getDataFromObj . The expression inside that parameter list extracts the contents of the zeroth element in the array object that is referred to by thecontents of the variable named objRef .

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