Java gurus ... can you figure this one out?

clockhar

Senior member
Dec 29, 2000
271
0
0
I can declare an array of a class type I made. However, I need to pass some values to the constructor, so I pass a few parameters to a working constructor. However, I cannot figure out how declare an array of my class that passes parameters. Can anyone help me out?

i.e. class name[] = new class (param 1, param2)[6];

Anyone have any ideas 'cause Java's complier keeps complaining on this syntax (I know the declaration works without specifying it as an array).

hmm ... after spending nearly an hour trying to figure this out, I find out 2 min after I post here. So, I found it. If anyone else wants to know, pm me.

 

manly

Lifer
Jan 25, 2000
13,589
4,239
136
Declaring an array in Java just allocates the storage for the array; it doesn't initialize the actual objects in the array.

So what you'll need to do to initialize is:

for (int i = 0; i < name.length; i++) {
name[ i ] = new MyClass(param1, param2);
}

Java is a pretty good language (primarily due to great libraries), but it is somewhat verbose with code. You'll overcome this by factoring your objects into many small methods that increases readability.