Either create the array when calling the constructor (inline):
new Unit("myname", new double[]{1.0,2.0},...);
or restructure your constructor to use varargs:
public Unit(String name, int weight, int strength, int agility, int toughness,
int currentHealth, int currentStamina, double... initialPosition) { ... }
//call
new Unit("myname", w,s,a,t,c,stam, 1.0, 2.0 );
However, I assume you need a specific number of coordinates for the position so I'd not use an array but an object for that:
class Position {
double x;
double y;
Position( x, y ) {
this.x = x;
this.y = y;
}
}
public Unit(String name, Position initialPosition, int weight, int strength, int agility, int toughness,
int currentHealth, int currentStamina ) { ... }
//call:
new Unit( "myname", new Position(1.0, 2.0), ... );
Advantages over using an array:
- It is typesafe, i.e. you pass in positions and not some arbitrary array of doubles. This prevents bugs where you accidentially pass in some other array.
- It defines the number of coordinates at compiletime, i.e. you know the number of coordinate a position has (2 in my example) whereas when using an array (or varargs which is basically the same) you could pass any number of coordinates (0 to Integer.MAX_VALUE).