Initialize an Array in Constructor in Java

Mohammad Irfan Jan 30, 2023 Sep 26, 2021
  1. Initialize Array in Constructor in Java
  2. Initialize Array in Constructor With New Values
  3. Initialize Array in Constructor in Java
Initialize an Array in Constructor in Java

This tutorial introduces how to initialize an array in constructor in Java and also lists some example codes to understand the topic.

An array is an index-based data structure that is used to store similar types of data. In Java, we can use an array to store primitive and object values. An array is also an object in Java and initialized with default values. For example, 0 for int, 0.0 for float/double, and null for String/object values.

If an array is declared as an instance variable, it gets initialized with default values when the object is called. Let’s see some examples.

Initialize Array in Constructor in Java

Initializing an array in the constructor does not make sense if it is initialized with default values because Java does this implicitly.

In this example, we declared an array in the class and then initialized it within a constructor, So, the array get initialized when the constructor is called. See the example below.

public class SimpleTesting{
    int a[];
    public SimpleTesting() {
        a  = new int[]{0,0,0};
    }
    public static void main(String[] args){
        SimpleTesting st = new SimpleTesting();
        System.out.println("Array Elements");
        // Accessing elements
        for (int i : st.a) {
            System.out.println(i);
        }
    }
}

Output:

Array Elements
0
0
0

We can do the above task without using constructor and see we get the same output for both code examples. We did not mention the initialization value here, but Java does this for us implicitly. See the example below.

public class SimpleTesting{
    int a[] = new int[3];
    public static void main(String[] args){
        SimpleTesting st = new SimpleTesting();
        System.out.println("Array Elements");
        // Accessing elements
        for (int i : st.a) {
            System.out.println(i);
        }
    }
}

Output:

Array Elements
0
0
0

Initialize Array in Constructor With New Values

Initialization using the constructor is a good idea if you want to set new values except for default. In this example, we pass other values, and the array gets initialized when the constructor is called. See the example below.

public class SimpleTesting{
    int a[];
    public SimpleTesting() {
        a  = new int[]{5,5,5};
    }
    public static void main(String[] args){
        SimpleTesting st = new SimpleTesting();
        System.out.println("Array Elements");
        // Accessing elements
        for (int i : st.a) {
            System.out.println(i);
        }
    }
}

Output:

Array Elements
5
5
5

Initialize Array in Constructor in Java

We can create an array in constructor as well to avoid the two-step process of declaration and initialization. It will do the task in a single statement. See, in this example, we created an array inside the constructor and accessed it simultaneously to display the array elements. See the example below.

public class SimpleTesting{
    public SimpleTesting() {
        int a[] = {0,0,0};
        System.out.println("Array Elements");
        // Accessing elements
        for (int i : a) {
            System.out.println(i);
        }
    }    
    public static void main(String[] args){
        SimpleTesting st = new SimpleTesting();
    }
}

Output:

Array Elements
0
0
0

Related Article - Java Array