Why can array constants only be used in initializers?

This question already has answers here:Closed 9 years ago.

Possible Duplicate:
Arrays constants can only be used in initializers error

I was studying arrays, and I came through this short-cut method of declaring and initializing an array in one line. For example,

int[] a = {1, 2, 3, 4, 5};

But when I tried to do following code, I got this compiler error saying “Array constants can only be used in initializer”.

int[] a;
a = {1, 2, 3, 4};

Why so?java

Leave a Comment