---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 24 Edition"
java_version: 24
chapter: "Chapter 10. Arrays"
section: "10.6"
heading: "10.6. Array Initializers"
parent: "Chapter 10. Arrays"
source: https://docs.oracle.com/javase/specs/jls/se24/html/jls-10.html#jls-10.6
id: "10.6"
url: https://jvm.obfuscated.software/jls/se24/10.6.html
raw: https://jvm.obfuscated.software/jls/se24/10.6.md
---

# 10.6. Array Initializers

An *array initializer* may be specified in a field declaration ([§8.3](8.3.md), [§9.3](9.3.md)) or local variable declaration ([§14.4](14.4.md)), or as part of an array creation expression ([§15.10.1](15.10.1.md)), to create an array and provide some initial values.

```
ArrayInitializer:
  { [VariableInitializerList] [,] }

VariableInitializerList:
  VariableInitializer {, VariableInitializer}
```

The following production from [§8.3](8.3.md) is shown here for convenience:

```
VariableInitializer:
  Expression
  ArrayInitializer
```

An array initializer is written as a comma-separated list of expressions, enclosed by braces `{` and `}`.

A trailing comma may appear after the last expression in an array initializer and is ignored.

Each variable initializer must be assignment-compatible ([§5.2](5.2.md)) with the array's component type, or a compile-time error occurs.

It is a compile-time error if the component type of the array being initialized is not reifiable ([§4.7](4.7.md)).

The length of the array to be constructed is equal to the number of variable initializers immediately enclosed by the braces of the array initializer. Space is allocated for a new array of that length. If there is insufficient space to allocate the array, evaluation of the array initializer completes abruptly by throwing an `OutOfMemoryError`. Otherwise, a one-dimensional array is created of the specified length, and each component of the array is initialized to its default value ([§4.12.5](4.12.5.md)).

The variable initializers immediately enclosed by the braces of the array initializer are then executed from left to right in the textual order they occur in the source code. The *n*'th variable initializer specifies the value of the *n*-1'th array component. If execution of a variable initializer completes abruptly, then execution of the array initializer completes abruptly for the same reason. If all the variable initializer expressions complete normally, the array initializer completes normally, with the value of the newly initialized array.

If the component type is an array type, then the variable initializer specifying a component may itself be an array initializer; that is, array initializers may be nested. In this case, execution of the nested array initializer constructs and initializes an array object by recursive application of the algorithm above, and assigns it to the component.

**Example 10.6-1. Array Initializers**

```
class Test {
    public static void main(String[] args) {
        int[][] ia = { { 1, 2 }, null };
        for (int[] ea : ia) {
            for (int e: ea) {
                System.out.println(e);
            }
        }
    }
}
```

This program produces the output:

```
1
2
```

before causing a `NullPointerException` in trying to index the second component of the array `ia`, which is a null reference.
