aspose file tools*
The moose likes Java in General and the fly likes Array Shortcut Notation as a Method Parameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Array Shortcut Notation as a Method Parameter" Watch "Array Shortcut Notation as a Method Parameter" New topic
Author

Array Shortcut Notation as a Method Parameter

Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
This works:



...as does this:



This does not:



The latter gives me a compiler exception stating that the method setParameters(String[]) is not applicable for the arguments (String, String, String).

Any ideas as to why the shortcut syntax is not allowed when used as a parameter to a method?

On a side note, I've gotten around the issue by changing the signature of generator.setParameters to be:



That allows me to do this:



I'm just curious why the notation above doesn't work in that scenario. Thanks.


SCJP Tipline, etc.
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

generator.setParameters({"server_name=server","db_name=dbname","dictionary_table_name=dd"});

This type of pattern is legal only during the initialization of Objects...


SCJP 6 [SCJP - Old is Gold]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Ram Narayan.M wrote:This type of pattern is legal only during the initialization of Objects...


That's right; for something even more surprising try this:


You'll find that doesn't compile because the second line doesn't initialize anything.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array Shortcut Notation as a Method Parameter