• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About Array Constants can only be used in initializers

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am trying to sort a given array .



I have commented out the line a above , because if i use the variable a , it is showing an compile time error as Array Constants can only be used in initializers

Please tell me why is it so .
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a more simpler way :


why the above is not possible (The same error)
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because what you're trying to do isn't an initializer.

If you said a = new String[] { ... } it would work (I think), but I'm not a fan of assigning parameters, as it can lead to poor thinking about what's actually happening.
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because with "a = {...}" it is not clear to the compiler what the type of "a" is. Consider:
It could look at the declared type (so Object[]) but because it's still ambiguous the compiler disallows it.

Fixed the . vs , error inside my initializer...
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like Rob's answer better ;)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic