Weird problem with static arrays

M

m.surion

I'm having errors with static arrays, errors which seem very weird to
me, here is the code which is causing the problem and the error
message. Is it my programming mistake, or is it some problem with
compiler or something?



static int[] wagi = new int[8]; //-line 10
wagi[4]=33; //-line 11


']' expected //-both
errors show error at line 11
<identifier> expected
 
E

Eric Sosman

I'm having errors with static arrays, errors which seem very weird to
me, here is the code which is causing the problem and the error
message. Is it my programming mistake, or is it some problem with
compiler or something?



static int[] wagi = new int[8]; //-line 10
wagi[4]=33; //-line 11


']' expected //-both
errors show error at line 11
<identifier> expected

Executable statements like `wagi[4]=33;' must
appear inside methods or constructors or initializer
blocks. You can't just drop them amid the field
declarations.

static int[] wagi = new int[8];
{
wagi[4] = 33;
}
 
R

Roedy Green

static int[] wagi = new int[8]; //-line 10
wagi[4]=33;

You did not show the context, but you must declare statics OUTSIDE
methods but INSIDE classes.
 
T

Thomas Hawtin

Eric said:
Executable statements like `wagi[4]=33;' must
appear inside methods or constructors or initializer
blocks. You can't just drop them amid the field
declarations.

static int[] wagi = new int[8];
{
wagi[4] = 33;
}

I assume what was meant was:

static int[] wagi = new int[8];
static {
wagi[4] = 33;
}

The top code has an instance initialise initialising a static instance,
which is probably a mistake (certainly sounds like one).

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top