I'm writing my firsts classes in java.
I did a class with an int[] that must be declared with 3 elements.
public abstract class MyClass {
private String name;
private int[] myValues = new int[2];
now my question is:
Then.. The constructor would be the best way to do that, so I did the constructor with every single element clearly required:
public MyClass(String nam, int val0, int val1, int val2){
int[] values = new int[]{val0, val1, val2};
setMyValues(values);
}
private void setMyValues(int[] vals){
this.myValues = vals;
}
it's a good practice? seems to be complicated instead of giving a simple array as:
public void MyClass(int[] vals)..
but in this way I can't be sure about number of elements.I should create a custom-exception or an if(vals.length != 2) cicle..
I've some other doubts:
is useful to declare myValues = new int[2] or it's the same simply writing int[] myValues? (why declare number of elements in the inner state?)
it's better to receive parameters from constructor or pass a vector to setter (setMyValues(int[] vals)) and check array in setter?
tank you for every suggestion.
Hope this help.