animeman420
animeman420

Reputation: 93

Very basic - Java array as class variable

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?

Upvotes: 7

Views: 20998

Answers (3)

Xueshi
Xueshi

Reputation: 114

class Foo{
     int[] doop;

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

Upvotes: 3

Vladimir Ivanov
Vladimir Ivanov

Reputation: 43108

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(){

     }
}

Upvotes: 4

wjans
wjans

Reputation: 10115

Try this:

this.doop= new int[]{1,2,3,4,5};

Upvotes: 12

Related Questions