• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

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

clockhar

Senior member
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.

 
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.
 
Back
Top