What does the ... syntax mean?

I am looking through some source code and I have come accross the following method signature

publicstatic SCADomain newInstance(String domainURI, String contributionLocation, String... composites){

What does the ... syntax after the type on the last parameter mean? ... seems to be a very difficult string to search for on the internet and in forums

[462 byte] By [bubblenut2a] at [2007-11-27 10:22:13]
# 1

http://java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html

aniseeda at 2007-7-28 17:14:44 > top of Java-index,Java Essentials,Java Programming...
# 2

If you have a parameter in a method folowed by ... then it means that the variable may, or may not, be passed in.

So in your example you could call the method like so: -

SCADomain.newInstance(domainURI, contributionLocation)

or you could call it like so: -

SCADomain.newInstance(domainURI, contributionLocation, composite)

or even like so: -

SCADomain.newInstance(domainURI, contributionLocation), composite1, composite2, composite3)

Basically the composite variable will either be null or a single String or an array of Strings

c0demonk3ya at 2007-7-28 17:14:44 > top of Java-index,Java Essentials,Java Programming...
# 3

It is saying zero or more arguments of type String.

public class StringTest {

public static void main(String[] args) {

doSomething(new String [] {"one", "two"} );

doSomething("one", "two");

doSomething();

}

static void doSomething(String ... strings) {

for(String s : strings) {

System.out.println(s);

}

}

}

Some of the benefits are you don't have to construct and array, you pass in your arguments like I have in second call. It will not complain if you do pass in an array and you can also pass in no-args.

It is basically an array with a little added flexibilty.

You can also do this (though personally I would not really advise it)

public static void main(String ... args)

_helloWorld_a at 2007-7-28 17:14:44 > top of Java-index,Java Essentials,Java Programming...
# 4

> You can also do this (though personally I would not

> really advise it)

>

> > public static void main(String ... args)

>

Why wouldn't you advise it?

georgemca at 2007-7-28 17:14:44 > top of Java-index,Java Essentials,Java Programming...
# 5

> Why wouldn't you advise it?

Well It goes against the norm I guess, also I can't think of one example where I have seen this used which tells me that this is not being adopted by other people.

The main benifit of the new syntax for me is the ability to call this method in different ways, on the command-line (where the majority, but not all of mains are called from I would bet) it provides no benefit.

I am not saying it is bad, I just don't have a reason to use it and think it is better to stick with the normal approach. I still think a lot of people are not up to date with Java 1.5.

_helloWorld_a at 2007-7-28 17:14:44 > top of Java-index,Java Essentials,Java Programming...
# 6

> > Why wouldn't you advise it?

>

> Well It goes against the norm I guess, also I can't

> think of one example where I have seen this used

> which tells me that this is not being adopted by

> other people.

I use it. I've seen it used by other people in my team, and on other teams in our department. It's not "the norm" because a lot of people - even quite experience developers - think there's something magical about the main method, and they don't want to tinker with it. But it works perfectly fine, it's no different to the other idiom

> The main benifit of the new syntax for me is the

> ability to call this method in different ways,

> on the command-line (where the majority, but not all

> of mains are called from I would bet) it provides no

> benefit.

>

> I am not saying it is bad, I just don't have a reason

> to use it and think it is better to stick with the

> normal approach. I still think a lot of people are

> not up to date with Java 1.5.

Avoiding Java 5 constructs won't change that!

georgemca at 2007-7-28 17:14:44 > top of Java-index,Java Essentials,Java Programming...