File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array Initilization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array Initilization" Watch "Array Initilization" New topic

Array Initilization

siva krishnan

Joined: Apr 26, 2003
Posts: 21
I am getting the error "illegal start of expression" while initializing the array at Line 7. Code is given below.

Am i missing any basic rule? Please help.

class ArrayTest {

public static void main(String [] args)

int[][] A1D = new int [2][];
byte b = 10;
char c = 'C';
A1D[0] = new int[]{1,2,3};
A1D[1] = {1,2}; //Line 7

int [][] B1D ;
B1D = A1D;

for(int i = 0;i<B1D.length;i++)
for (int j = 0;j<B1D[i].length;j++)
System.out.println("element in the array A :" + B1D[i][j]);



Cheers<br />Siva<br />SCJP 1.4<br />SCWCD 1.3
vydhehi paruchuri

Joined: Aug 27, 2003
Posts: 22

If you are using array initialize list,{1,2} to initialize an array then array should be declared in the same statement.

AID[1]= {1,2} //will not work causes a compile time error.

int[1] newArray={1,2} //this will work

Hope this helps.


Mahesh Bhatt
Ranch Hand

Joined: Sep 15, 2004
Posts: 88
hi siva,

When you are using these type of arrays, the syntax you would have to use to initialize your array would have to be something like this :

hope it helps you .
[ September 20, 2004: Message edited by: prashant bhogvan ]

Impossible is I M Possible
Louie van Bommel
Ranch Hand

Joined: Aug 17, 2004
Posts: 76
Originally posted by siva krishnan:

int[][] A1D = new int [2][];
byte b = 10;
char c = 'C';
A1D[0] = new int[]{1,2,3};
A1D[1] = {1,2}; //Line 7


An array is created by an array creation expression (�15.10) or an array initializer (�10.6).
An array initializer may be specified in a declaration, or as part of an array creation expression (�15.10), creating an array and providing some initial values...

An array initializer can only be part of a declaration. You already have a declaration in the first line (of the code) above. That's the declaration. If you want to use an initializer, it has to be in its declaration.

Trying to modify your example by throwing in an "int[]" won't help either:

This will compile:

So you'd think you could apply that to A1D[1] instead, but alas:

The reason in this example is that the compiler is expecting a declaration statement which could have int[]b = {<something>}; or int[][]c = {<something else>};

In your case, the array named A1D has already been declared and can't be declared again.

It's interesting to look at this (which works):

but you have to see that this is not really an initializer. You are creating an anonymous array, and then assigning it to a variable. It's kinda equivalent to:

I agree. Here's the link:
subject: Array Initilization
It's not a secret anymore!