The moose likes Beginning Java and the fly likes dynamic method parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "dynamic method parameters" Watch "dynamic method parameters" New topic

dynamic method parameters

Trent DiBacco

Joined: May 24, 2002
Posts: 18
Is there a way to define a method to accept as many arguments as posible.
I am not talking about overloading either.
I thinking more on the lines like the main method
public static void main(String[] args)
System.out.println( arg + arg + arg )
where it is undeterminable how many argument you will be accepting.
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
The answer to your question is in your question itself:
Trent DiBacco

Joined: May 24, 2002
Posts: 18
I know that you can pass an array, but i dont want the user to have to make an array in order to pass a list of argument to my method.
I want it to be something like
the println method
where one call to the method might be
and some other call to the method might be
someMethod( arg1, args2 args3 )
i dont want them to have to make and array of arguments and then pass my the reference to that array
Trent DiBacco

Joined: May 24, 2002
Posts: 18
I'll will try to make this more clear.

[ May 25, 2002: Message edited by: Trent Green ]
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6529

There's no way to do this in Java. You have to either accept an array or write many overloaded versions of your method. If you study it more closely, you'll realize that the all the overloaded versions of println() method really take only one parameter. It just seems that it allows many parameters because of the way the + operator is overloaded for Strings.
It's not really that much of a bother to pass an array to a method. Consider these examples:
foo(new int[] {1, 2, 3, 4, 5});
foo(new Object[] {"A String", new Object(), new java.util.ArrayList()});
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Of course C# supports that.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Trent DiBacco

Joined: May 24, 2002
Posts: 18
Thank you very much Ilja Preuss. The link provided above was very informative. Thanks to all the other "Rancher Hands" too.
I really love this site. It is one of the few special places on the net.
[ May 25, 2002: Message edited by: Trent Green ]
I agree. Here's the link:
subject: dynamic method parameters
It's not a secret anymore!