Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anonymous Array

 
Bindesh Vijayan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code :

When executed gets the following error:

Array constants can only be used in initializers.
print( {new Object(),new Object(),new Object()},
Array constants can only be used in initializers.
{"A1","A2","A3","A4"}

Am iam not using an initializer.JLS has the following statement

Pls. help..
Thanks
Bindesh Vijayan
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try run the code like this:

/Rene
 
Val Lee
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I modify your code:

Anonymous array used like this:new int[]{1, 2, 3}
or new int[10].
 
Bindesh Vijayan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,
First things first, Thanks, and a correction I have missed some parts of the code as Rene points out.All I can say is it happens sometimes.
And yes coming back to the problem,I knew this that the array should be given with new
operator,thats not what I wanted to know ,but the specification provided by JLS .
An array initializer is written as a comma-separated list of expressions, enclosed by braces "{" and "}".

I want to be cleared on that...
Thanks
Bindesh
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This initialize an one dimension array:

This initialize a two dimension array:

/Rene
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic