Bug 187 : Some simple code doesn't work
Last modified: 2005-10-25 06:25




Status:
RESOLVED
Resolution:
INVALID -
Priority:
P2
Severity:
normal

 

Reporter:
adepali
Assigned To:
fry

Attachment Type Created Size Actions

Description:   Opened: 2005-10-25 05:06
This code won't compile:

int[] num = new int[ 5 ];
num[ 0 ] = 3;

void hello( )
{
println("HI");
}

I get an unexpected token: void. It always happens when i use any kind of
dynamic allocation. Changing the order like this:

void hello( )
{
println("HI");
}

int[] num = new int[ 5 ];
num[ 0 ] = 3;

I get an "Expecting RBRACK, found '0'" error. The allocation works fine if
specified inside a function block, but not in the global environment.
Additional Comment #1 From fry 2005-10-25 06:25
that's just a bug in your code. except for in special cases, you cannot
assign the contents of arrays outside of methods.

int[] num = new int[ 5 ];
num[ 0 ] = 3; // this line is just bad syntax

void hello( )
{
println("HI");
}

to fix this, use:

int[] num = new int[ 5 ];

void hello( )
{
num[ 0 ] = 3; // it's ok to do this here
println("HI");
}