First there are a few compilation errors in your code before that line. Here is the correct version (until the line you asked):

Now to your question:

You can only initialize variables outside a method in a class when you declare them. Because you declared the array in one line (you initialized it there), you can't assign to it stuff in other line if they are not in a method. Same goes for the classid variable. You can however do these linea:

Notice that this way we initialize the variables in the same line we create them. Otherwise, you have to that in the contsructor or in a method.

When declaring a variable, you can assign a value at the point of declaration. But note that the variable here is "studentids," so you're providing a "value" (an object reference) for the array itself -- not the values (references) it holds.

Note that in the case of arrays, you have the option of using a "short-cut" syntax...

int[] studentIDs = { 10, 20, 30, 40, 50 };

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummersscce.org

Soma RaviKumar

Greenhorn

Posts: 6

posted 12 years ago

Thank you very much for your quick reply. I have one more question regarding this. Is an array created on STACK or on HEAP ? I know that the objects are created on HEAP and the references to it reside on STACK. In the case of arrays, what is on STACK and what is on HEAP? Thanks again.