Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie: Init an array object 1

Status
Not open for further replies.

nat101

Programmer
Jul 5, 2001
147
US
I'm missing something as how to initialize arrays, specifically how an integer array differs from a class array, in this respect.
If I have a class myclass and would like to create a myclass array of 5 elements/objects, why doesn't the following work:
myclass[] myclassArray = new myclass[5];
I would think that myclassArray has 5 myclassobjects ready to be used with myclassArray[x].mystring="TEST"; (x=0 thru 4).
In reality though myclassArray is null and throws an exception.

TIA
-Nat
 
You do not have an array of 5 objects you have an array of 'pointers' to objects. You must instantiate each one seperately:
Code:
myclass[] myclassArray = new myclass[5];
myclassArray[x] = new myclass();

Good Luck

Wiszh
 
You are correct. Initializing your array does not populate it with objects. You will have to add the objects yourself.
 
Thank you! It clicked.
So the question/observation is WHY doesn't it create the objects just with the:
myclass[] myclassArray = new myclass[5];
What other reason would I use the above line.

SO, this leads me to believe that Java allows different classes in each element of the array. Please advise on the following.

If I have myclass, myclassThis, myclassThat, myclassOther.

myclass[] myclassArray = new myclass[5];
myclassArray[0] = new myclass();
myclassArray[1] = new myclassThis();
myclassArray[2] = new myclassThat();
myclassArray[3] = new myclassOther();
If this works, I shall call it 'interesting'.

Thanks again
-Nat
 
No, Java does NOT allow different classes in each element of the array. That is why you declare it as an array of myclass references.

Wiszh points out that the array only holds REFERENCES to objects, not the objects themselves. Initially those object references are null. It is the same as if you did:

myclass mc;

for each element in the array.

Now notice that when you initialize an array of primitives, they are given a default value. This is because the array element holds the actual value and not an object so the runtime can actually put a default value there. This is not the case with an object because there may be no default CTOR for the objects that you are putting in the array and the runtime would have no idea how to construct an object to put there.

The example you give above won't work unless ALL of those classes are of the same type (e.g. extend from myclass).
 
Ok. I suspected so. But then the question is why/when would someone want to do this:
myclass[] myclassArray = new myclass[5];
without this:
myclassArray[x] = new myclass();

The java designers must of had a reason for not automating the process into one command.
Thanks again.


 
Maybe you want to create an array to hold myclass objects but you don't have any to put in it yet. For instance, an ArrayList creates an underlying object array that is initially empty to hold the objects that you are going to put in the list:

Object [] o = new Object[10];
 
And like meadandale said, you can have an array that contains various extensions of your class. This would be useful for performing the same task on a variety of objects of different classes that all extended one class. A textbook example:

Shape[] shapes = new Shape[3];

// Assuming Circle, Square, and Triangle all extend Shape
shapes[0] = new Rectangle();
shapes[1] = new Square();
shapes[2] = new Triangle();

// Sets the height of three different classes
for( int i = 0; i < 3; i++ ) {
shapes.height = 50;
}

This clearly has its advantages in certain situations, like GUIs, where you'll have many container classes and many component classes.
 
is the beginning tag for italics in the tag language used on this site. When you tried shapes.height it recoginized it as italics since your code was not placed within
Code:
tags and you didn't turn off TGML processing.

Fairly common. It actually helps to place your code samples within the
Code:
 tags for readability as well.
 
Aye. That's good news! When I wrote my example with myclass, myclassthis, myclassthat.... I was hoping someone would tell me that as long as myclassxxxxx were subclasses of myclass, it is ok.
So this means that an array class defined for a supeclass, allows any subclass be defined/created in an element.
-Nat
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top