wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes About Array Constants can only be used in initializers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "About Array Constants can only be used in initializers" Watch "About Array Constants can only be used in initializers" New topic
Author

About Array Constants can only be used in initializers

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

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 .


Save India From Corruption - Anna Hazare.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

In a more simpler way :


why the above is not possible (The same error)
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Oct 27, 2005
Posts: 19679
    
  18

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...


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I like Rob's answer better ;)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About Array Constants can only be used in initializers