Actually we can! Just wrap those statements within a curly braces block:
Class[] array = new Class[10];
{
for (int i = 0; i < array.length; ++i) {
array[i] = new Class(0, i*50);
}
}
Those are called initialization blocks btW.
And they’re run before their class’ constructors, together w/ field declarations.
Statements outside any initialization blocks, functions and field declarations aren’t allowed in Java.
Class’ variables are called fields in Java.
In Java, all class members need to be well-defined.
Members can’t be added on-the-fly to a class later.
And variables declared within methods aren’t fields, but local variables.