Hi everybody, just registered. I'm studying java and thers is something about array creation and inizialization that seems weird to me.
class Test { private int[] array; array = new int[2]; }
Hi everybody, just registered. I'm studying java and thers is something about array creation and inizialization that seems weird to me.
The following code gives an error at compile
class Test { private int[] array; array = new int[2]; //<identifier> expected at compile time }
But if I do the same inside a method, it works
class Main { public static void main(String[] args) { int[] arrayM; arrayM = new int[2]; array[0] = 1; array[1] = 2; } }
And an other strangeness I can't figure out is that if I modify the first snippet as in the following, it works:
class Test { private int[] array = new int[]{1,2}; }
Thanks in advance.
AR
I apologize for this post. It has been submitted by mistake.
Please refer to the correct one:
http://www.javaprogrammingforums.com...ion-class.html