I have a class and in that class I have this:
//some code
private int[] data = new int[3];
//some code
Then in my constructor:
public Date(){
data[0] = 0;
data[1] = 0;
data[2] = 0;
}
If I do this, everything is OK. Default data values are initialized but if I instead do this:
public Date(){
int[] data = {0,0,0};
}
It says:
Local variable hides a field
Why?
What’s the best way to initialize an array inside the constructor?
This already initializes your array elements to 0. You don’t need to repeat that again in the constructor.
In your constructor it should be: