A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
Arduino in Action
this week in the
General Computing
forum!
A special promo:
Enter your blog post or vote on a blogger to be featured in an upcoming Journal
JavaRanch
»
Java Forums
»
Java
»
Java in General
Author
Array Shortcut Notation as a Method Parameter
Corey McGlone
Ranch Hand
Joined: Dec 20, 2001
Posts: 3271
posted
Aug 31, 2010 14:24:25
0
This works:
String[] args = {"server_name=server","db_name=dbname","dictionary_table_name=dd"}; generator.setParameters(args);
...as does this:
generator.setParameters(new String[] {"server_name=server","db_name=dbname","dictionary_table_name=dd"});
This does not:
generator.setParameters({"server_name=server","db_name=dbname","dictionary_table_name=dd"});
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:
generator.setParameters(String... args)
That allows me to do this:
generator.setParameters("server_name=server","db_name=dbname","dictionary_table_name=dd");
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
I like...
posted
Aug 31, 2010 15:11:26
0
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: 16487
2
I like...
posted
Aug 31, 2010 18:19:46
0
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:
String[] args; args = {"server_name=server","db_name=dbname","dictionary_table_name=dd"};
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
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter