Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Very basic - Java array as class variable

Tags:

java

arrays

class Foo{
     int[] doop;

     public Foo(){
          this.doop={1,2,3,4,5};
     }
}

I can't compile this, Java ME SDK gives me a bunch of "Illegal Start of Expression" errors. Why? How do I make this work?

like image 956
animeman420 Avatar asked May 31 '11 06:05

animeman420


2 Answers

Try this:

this.doop= new int[]{1,2,3,4,5};
like image 134
wjans Avatar answered Jan 11 '23 17:01

wjans


You can't do this in constructor, because this syntax is allowed only for declaration with initialization. Fix to this:

class Foo{
     int[] doop = new int[]{1,2,3,4,5};

     public Foo(){

     }
}
like image 23
Vladimir Ivanov Avatar answered Jan 11 '23 15:01

Vladimir Ivanov