<< Chapter < Page Chapter >> Page >

For example, if you declare the array to contain references of type Object , those references can refer to any type of object (including array objects) because a reference of type Object can be used to refer to any object.

You can do similar things using interface types. I will discuss interface types in a future module.

Often need to downcast to use an Object reference

If you store all of your references as type Object , you will often need to downcast thereferences to the true type before you can use them to access the instance variables and instance methods of the objects.

Doing the downcast no great challenge as long as you can decide what type to downcast them to.

The Vector class

There is a class named Vector that takes advantage of this capability. An object of type Vector is a self-expanding array of reference variables of type Object . You can use an object of type Vector to manage a group of objects of any type, either all of the same type, or mixed.

(Note that you cannot store primitive values in elements of a non-primitive or reference type. If you need to do that, you will need to wrapyour primitive values in an object of a wrapper class as discussed in an earlier module.)

A sample program using the Date class

The sample program, named array03 and shown in Listing 3 isn't quite that complicated. This program behaves as follows:

  • Declare a reference variable to an array of type Date . (The actual type of the variable is Date[].)
  • Instantiate a three-element array of reference variables of type Date .
  • Display the contents of the array elements and confirm that they are all null as they should be. (When created using this syntax, new array elements contain the default value, which is null for reference types.)
  • Instantiate three objects of type Date and store the references to those objects in the three elements of the array.
  • Access the references from the array and use them to display the contents of the individual Date objects.

As you might expect from the name of the class, each object contains information about the date.

Listing 3 . The program named Array03 .
/*File array03.java Copyright 1997, R.G.Baldwin Illustrates use of arrays with objects.Illustrates that "an array of objects" is not really an array of objects, but rather is an array of referencesto objects. The objects are not stored in the array, but rather are stored somewhere else in memory and thereferences in the array elements refer to them. The output from running this program is:myArrayOfRefs contains nullnull nullmyArrayOfRefs contains Sat Dec 20 16:56:34 CST 1997Sat Dec 20 16:56:34 CST 1997 Sat Dec 20 16:56:34 CST 1997**********************************************************/ import java.util.*;class array03 { //define the controlling class Date[]myArrayOfRefs; //Declare reference to the array array03() {//constructor//Instantiate the array of three reference variables // of type Date. They will be initialized to null.myArrayOfRefs = new Date[3];//Display the contents of the array. System.out.println( "myArrayOfRefs contains" );for(int cnt = 0; cnt<3; cnt++) System.out.println(this.myArrayOfRefs[cnt]); System.out.println();//Instantiate three objects and assign references to// those three objects to the three reference // variables in the array.for(int cnt = 0; cnt<3; cnt++) myArrayOfRefs[cnt]= new Date(); }//end constructor//-----------------------------------------------------//public static void main(String[] args){//main methodarray03 obj = new array03(); System.out.println( "myArrayOfRefs contains" );for(int cnt = 0; cnt<3; cnt++) System.out.println(obj.myArrayOfRefs[cnt]); System.out.println();}//end main }//End array03 class.

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