In reading some books on
Java Array declaring then writting small files to try and compile I did found that if I declare an Array reference outside of a method, then try to create the array using the array ref = new array_type[size], the program will not compile, but if I do the same inside a method it works
Example (file named Arraytest.java)
class Arraytest{
int[] scores = new int[5]; // NOT in method declare and create on 1 line
int[] scores2;
scores2 = new int[5]; // This will NOT compile
public anyMethod(){
int[] scores3;
scores3 = new int[5]; //This will compile
}
}
Why is this??? I have a feeling it has to do with no variables are created until the class is created, but then why if you do it on 1 line it compiles fine.
All the books I that talk about arrays use code snipets
and explain that doing declaring and creating array on two different lines is find.
FYI running Java 1.3