0

I'm studying this piece of code, it is a constructor with several parameters. The declaration of the last parameter with ... What does this mean?

    /**
 * Public constructor.
 * @param servicePort the service port
 * @param nodeAddresses the node addresses
 * @param sessionAware true if the server is aware of sessions, false otherwise
 * @throws NullPointerException if the given socket-addresses array is null
 * @throws IllegalArgumentException if the given service port is outside range [0, 0xFFFF],
 *    or the given socket-addresses array is empty
 * @throws IOException if the given port is already in use, or cannot be bound
 */
public TcpSwitch(final int servicePort, final boolean sessionAware, final InetSocketAddress... nodeAddresses) throws IOException {
    super();
    if (nodeAddresses.length == 0) throw new IllegalArgumentException();

    this.serviceSocket = new ServerSocket(servicePort);
    this.executorService = Executors.newCachedThreadPool();
    this.nodeAddresses = nodeAddresses;
    this.sessionAware = sessionAware;

    // start acceptor thread
    final Thread thread = new Thread(this, "tcp-acceptor");
    thread.setDaemon(true);
    thread.start();
}
1

2 Answers 2

6

It is called varargs, look here for more http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html

The three periods after the final parameter's type indicate that the final argument may be passed as an array or as a sequence of arguments. Varargs can be used only in the final argument position.

As you can see in your code in this case it is an array.

Sign up to request clarification or add additional context in comments.

Comments

3

The parameter :

final InetSocketAddress... nodeAddresses

means a variable arguments. It can take 1 or more variable with the same data type as parameter to the function.

See : http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.