KostasRim
KostasRim

Reputation: 2053

Method parameters in java

The only thing i know is that (...) indicates that Texture key is an optional parameter for the method.Are there any other uses ?

Texture[] key;                              //1
public Animation( Texture ... key ) {       //2
    this.key = key;                         //3
}

Upvotes: 0

Views: 60

Answers (3)

gtgaxiola
gtgaxiola

Reputation: 9331

Texture ... key does not denote optional parameters, but actually an unspecified number of arguments to a method. Java treats the variable-length argument list as an array.

More information on VarArgs

Upvotes: 3

ortis
ortis

Reputation: 2223

... indicates a varargs parameters. It's basically an array that can be empty.

So

Animation();

is a valid call, as well as

Animation(key1, key2);

Note that only one varargs parameter is allowed per method and that it must be the last parameter of the method

Upvotes: 2

brso05
brso05

Reputation: 13222

Texture ... key

This is used if you don't know how many parameters will be passed. key will be an array with the parameters that were passed to the Animation method. You can pass n number of parameters of type Texture.

Upvotes: 2

Related Questions