<< Chapter < Page | Chapter >> Page > |
A special instantiation syntax
There is a special syntax that allows for the instantiation of an array object and the initialization of the array elements in a single statement. (I explain this here , here , and here so you should already know all about it.) The last statement in Listing 1 is an example of this syntax.
Briefly, the syntax consists of a comma separated list of element values (expressions) inside a pair of matching curly braces. The length of the array is determined by the number of values in the list. The type of thearray is determined by the types of the elements in the list.
This syntax instantiates an array object of the correct length and populates the elements with the specified values.
A reference is returned
A reference to the array object is returned in much the same way that a constructor for an ordinary object returns a reference to the object.
As is always the case, if the reference is stored in a variable, the type of the reference must be assignment compatible with the type of the variable.
What is assignment compatible?
If you have forgotten what this term means, I recommend that you go to Google and search for the following keywords to learn more about it:
site:http://cnx.org/contents/ "assignment compatible" java
A one-element array
The last statement in Listing 1 instantiates an array object containing a one-element array. The array element is initializedwith a reference to a new object of type Prob05MyClassA , which exists somewhere else in memory.
The value of a random number that was generated earlier in the main method is passed as a parameter to the constructor for the object of type Prob05MyClassA .
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?