HOWTO · Java

How to Initialize an Array in Constructor in Java

This article is all about to initialize an array in constructor in Java.

On this page

When creating a Java object, initialize an array field in the constructor if its starting contents depend on constructor arguments or other setup. If Java’s defaults are suitable, allocating the field is enough. An array declared as a local variable inside the constructor is available only while that constructor runs; it does not become object state.

Initialize an Array Field in a Java Constructor

Declare the array as an instance field, then assign an array reference to it in the constructor. The constructor runs for each new object, so it can set the field from that object’s inputs or prepare values calculated for that object.

An array field and the array it refers to are separate things: the field holds a reference, while the array holds elements. Creating the array with new int[size] allocates its elements and gives each a default value. Assigning a local array to a field, such as this.values = values, stores that reference; use a copy when the object should own independent contents.

Use Java’s Default Values for an Array

Java assigns default values to instance fields before the constructor body runs. When the constructor creates an array, Java also initializes its elements: numeric primitive elements start at zero (0 for integral types and 0.0 for float and double), char elements start at \u0000, boolean elements are false, and reference elements such as String are null.

For example, private int[] values = new int[3]; creates an instance field with three zero-valued elements. You can instead declare private int[] values; and assign values = new int[3]; in the constructor; the elements still start at zero. Do not add a loop that writes those same values unless the assignment communicates a separate requirement.

Assign Specific Values in the Constructor

Use an array initializer for fixed contents, or accept values as constructor parameters when each object should start with caller-provided contents. For example, this.values = new int[] {1, 2, 3}; gives the field those three values. A parameter-based constructor can assign or copy its input, depending on whether later changes to the caller’s array should also affect the object.

The example below puts several approaches in one class. It creates a default-filled field, copies supplied values, fills another field with one value, calculates values from their indexes, and uses a local array during construction. The program displays each array so you can adapt the parts that match your class.

import java.util.Arrays;

public class ArraySetup {
    private final int[] defaults = new int[3];
    private final int[] supplied;
    private final int[] repeated = new int[3];
    private final int[] calculated = new int[3];

    public ArraySetup(int[] source) {
        supplied = Arrays.copyOf(source, source.length);
        Arrays.fill(repeated, 7);
        Arrays.setAll(calculated, index -> (index + 1) * 2);

        int[] local = {4, 5};
        System.out.println("Local: " + Arrays.toString(local));
    }

    public static void main(String[] args) {
        int[] input = {3, 6, 9};
        ArraySetup item = new ArraySetup(input);
        System.out.println("Defaults: " + Arrays.toString(item.defaults));
        System.out.println("Supplied: " + Arrays.toString(item.supplied));
        System.out.println("Repeated: " + Arrays.toString(item.repeated));
        System.out.println("Calculated: " + Arrays.toString(item.calculated));
    }
}

Create an Array Locally in the Constructor

A declaration such as int[] local = {4, 5}; inside a constructor creates a local variable. Code in that constructor can use the variable, but other methods cannot access it after the constructor returns. If the array is meant to be part of the created object’s state, assign it to an instance field instead, for example this.values = new int[] {4, 5};.

A local array can still be useful as temporary input while building a field, or for a calculation needed only during setup. Keep the distinction clear: the array object can remain reachable if a field or another object stores its reference, but a constructor-local variable alone does not preserve it.

Use Arrays.copyOf(), Arrays.fill(), or Arrays.setAll()

Choose the helper based on how the contents are determined. Arrays.copyOf(source, source.length) creates a separate array containing the source elements. For a shorter or longer requested length, it copies what fits; any additional elements in a longer copy receive their type’s default value. For object arrays, this is a shallow copy: references are copied, but referenced objects are not duplicated.

Use Arrays.fill(array, value) when every element should receive the same value. Use Arrays.setAll(array, index -> expression) when each value is computed from its index. In the example, the fill operation assigns 7 throughout the array, while the index function assigns successive even numbers. These methods update the array passed to them; they do not create a replacement array.

Choose an Initialization Approach

Use a sized array and rely on defaults when those values are the intended starting state. Use an array initializer for a small, fixed set of values, or constructor parameters when callers choose the contents. Copy an input array when the object needs its own array structure; use Arrays.fill() for one repeated value and Arrays.setAll() for index-based values. Declare the array as an instance field whenever it must remain available as object state after construction.