101

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?

1
  • I hope you are aware of the concept of scope of variable? your variable data after "//some code" is having a scope of the entire class. while the variable data in the constructor is having a scope inside the constructor. and once the constructor is done, the variable will be destroyed. And also the data variable inside constructor hides the variable by same name in the class. This justifies the error Local variable hides a field Commented Nov 10, 2011 at 13:08

7 Answers 7

195
private int[] data = new int[3];

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:

data = new int[]{0, 0, 0};
Sign up to request clarification or add additional context in comments.

2 Comments

Yes. This was the only way It allowed me. Putting just data; said that "its not a statement". Thanks
Can't he use data=new int[3] in defualt constructor Date()?
10

You could either do:

public class Data {
    private int[] data;

    public Data() {
        data = new int[]{0, 0, 0};
    }
}

Which initializes data in the constructor, or:

public class Data {
    private int[] data = new int[]{0, 0, 0};

    public Data() {
        // data already initialised
    }
}

Which initializes data before the code in the constructor is executed.

2 Comments

I have to put data = new int[] {0,0,0}; inside the constructor. If I initialize before the code in the constructor putting just data; inside the constructor gave me "not a statement" error
if there are more than million elements in array.. you would like to use first code and change line public Data() throws Error { if OutOfMemoryError will be thrown.
4

This is because, in the constructor, you declared a local variable with the same name as an attribute.

To allocate an integer array which all elements are initialized to zero, write this in the constructor:

data = new int[3];

To allocate an integer array which has other initial values, put this code in the constructor:

int[] temp = {2, 3, 7};
data = temp;

or:

data = new int[] {2, 3, 7};

Comments

4

why not simply

public Date(){
    data = new int[]{0,0,0};
}

the reason you got the error is because int[] data = ... declares a new variable and hides the field data

however it should be noted that the contents of the array are already initialized to 0 (the default value of int)

Comments

1

in your constructor you are creating another int array:

 public Date(){
  int[] data = {0,0,0};
  }

Try this:

 data = {0,0,0};

NOTE: By the way you do NOT need to initialize your array elements if it is declared as an instance variable. Instance variables automatically get their default values, which for an integer array, the default values are all zeroes.

If you had locally declared array though they you would need to initialize each element.

Comments

0

The best way is not to write any initializing statements. This is because if you write int a[]=new int[3] then by default, in Java all the values of array i.e. a[0], a[1] and a[2] are initialized to 0! Regarding the local variable hiding a field, post your entire code for us to come to conclusion.

Comments

0

Please remember that this will change the number of elements in the constructor. That is...

public class Test
{
   int[] arr =new int[4];
   int n = arr.length;
   Test()
   { 
       arr = new int[]{0,0,0,0,0,0,0};
       n = arr.length;
   }
 }

In the Constructor, int[]{0,0,0,0,0,0,0} will compile successfully even though the limit is 4 because of the new keyword. The final n value is 7.

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.