<< Chapter < Page | Chapter >> Page > |
A sample program named Array08
The sample program named Array08 (shown in Listing 12 near the end of the module) illustrates the use of some of these methods.
Will discuss in fragments
As usual, I will discuss this program in fragments. Essentially all of the interesting code is in the method named main , so I will begin my discussion there. The first few fragments will illustrate the creation,population, and display of a one-dimensional array object whose elements contain references to objects of type String .
The newInstance method of the Array class
The code in Listing 1 calls the static method of the Array class named newInstance to create the array object and to store the object's reference in a reference variable of type Object named v1 .
(Note that there are two overloaded versions of the newInstance method in the Array class. I will discuss the other one later.)
Listing 1 . Using the newInstance method. |
---|
Object v1 = Array.newInstance(
Class.forName("java.lang.String"),3); |
Two parameters required
This version of the newInstance method requires two parameters. The first parameter specifies the component type. This must be a reference to a Class object representing the component type of the new array object.
The second parameter, of type int , specifies the length of the new array object.
The Class object
The second parameter that specifies the array length is fairly obvious. However, you may need some help with the first parameter. Here is part of whatSun has to say about a Class object.
"Instances of the class Class represent classes and interfaces in a running Java application. Every array alsobelongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions. The primitiveJava types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects."
Getting a reference to a Class object
I know of three ways to get (or refer to) a Class object.
Class objects for primitive types
There are nine predefined Class objects that represent the eight primitive types and void. These are created by the Java Virtual Machine, andhave the same names as the primitive types that they represent: boolean , byte , char , short , int , long , float , and double . You can refer to these class objects using the following syntax:
I will illustrate this later in this module.
The getClass method
If you have a reference to a target object (ordinary object or array object), you can gain access to a Class object representing the class from which that object was instantiated by calling the getClass method of the Object class, on that object.
The getClass method returns a reference of type Class that refers to a Class object representing the class from which the target object was instantiated.
The forName method
The static forName method of the Class class accepts the name of a class or interface as an incoming String parameter, and returns the Class object associated with the class or interface having the given string name.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?