PDA

View Full Version : Java Question



sean
12-03-2001, 06:19 PM
In Java, you have to use the following notation for declaring instances of user-defined classes (I know this is blatently obvious if you know Java, but I'm just setting my self up for my point. It seems like I don't have one, I know, but I do):

class_type object_name = new class_type(constructor_parameter)

The book I'm using tells me that to declare arrays of user defined classes, you use the following notation:

class_type[] array_name = new class_type[array_size];

My question is this: where do the constructor parameters go? And how do you declare arrays of the primitive data types (i.e. int, etc.)? <- OK, THERE'S my point!

Thanks in advance if you can help.

_______________________
Sean Mackrory
sean_mackrory@hotmail.com

mix0matt
12-03-2001, 11:45 PM
jk, i'm not a java master. I'm just learning the larguage. it seems really interesting, though

to answer you're main "point" concerning the array of primitive types:



int[] array = new int[100]; // or...
int array2[] = new int [100];


it could go either way....

as f0r your first question, "where do the constructor parameters go". i don't really know. my guess is that it is similar to C++ in that the default constructor will be used.

SilentStrike
12-04-2001, 01:01 AM
public class whatever {
int data;
whatever(int i) {
data = i;
}
public static void main(String[] args) {
/* no actual whatevers created yet, whatever[0] to
whatever[9] contain null references, but whateverlist is
not null */
whatever[] whateverlist = new whatever[10];
for (int i = 0; i < 10; i++) {
whateverlist[i] = new whatever(i*i); // create individual whatevers here
}
}
}