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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply 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: 244

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: 16483
    
    2

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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Array Shortcut Notation as a Method Parameter
 
Similar Threads
JAR packaging problem
Final check before submission
getInitParameter
RMI start error
Syntax of Array as parameter in JDK 1.6