---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 26 Edition"
java_version: 26
chapter: "Chapter 15. Expressions"
section: "15.9.4"
heading: "15.9.4. Run-Time Evaluation of Class Instance Creation Expressions"
parent: "15.9. Class Instance Creation Expressions"
source: https://docs.oracle.com/javase/specs/jls/se26/html/jls-15.html#jls-15.9.4
id: "15.9.4"
url: https://jvm.obfuscated.software/jls/se26/15.9.4.html
raw: https://jvm.obfuscated.software/jls/se26/15.9.4.md
---

# 15.9.4. Run-Time Evaluation of Class Instance Creation Expressions

At run time, evaluation of a class instance creation expression is as follows.

First, if the class instance creation expression is a qualified class instance creation expression, the qualifying primary expression is evaluated. If the qualifying expression evaluates to `null`, a `NullPointerException` is raised, and the class instance creation expression completes abruptly. If the qualifying expression completes abruptly, the class instance creation expression completes abruptly for the same reason.

Next, space is allocated for the new class instance. If there is insufficient space to allocate the object, evaluation of the class instance creation expression completes abruptly by throwing an `OutOfMemoryError`.

The new object contains new instances of all the fields declared in the specified class and all its superclasses. As each new field instance is created, it is initialized to its default value ([§4.12.5](4.12.5.md)).

Next, the actual arguments to the constructor are evaluated, left-to-right. If any of the argument evaluations completes abruptly, any argument expressions to its right are not evaluated, and the class instance creation expression completes abruptly for the same reason.

Next, the selected constructor of the specified class is invoked. This results in invoking at least one constructor for each superclass of the class. This process can be directed by explicit constructor invocations ([§8.8.7.1](8.8.7.1.md)) and is specified in detail in [§12.5](12.5.md).

The value of a class instance creation expression is a reference to the newly created object of the specified class. Every time the expression is evaluated, a fresh object is created.

**Example 15.9.4-1. Evaluation Order and Out-Of-Memory Detection**

If evaluation of a class instance creation expression finds there is insufficient memory to perform the creation operation, then an `OutOfMemoryError` is thrown. This check occurs before any argument expressions are evaluated.

So, for example, the test program:

```
class List {
    int value;
    List next;
    static List head = new List(0);
    List(int n) { value = n; next = head; head = this; }
}
class Test {
    public static void main(String[] args) {
        int id = 0, oldid = 0;
        try {
            for (;;) {
                ++id;
                new List(oldid = id);
            }
        } catch (Error e) {
            List.head = null;
            System.out.println(e.getClass() + ", " + (oldid==id));
        }
    }
}
```

prints:

```
class java.lang.OutOfMemoryError, false
```

because the out-of-memory condition is detected before the argument expression `oldid = id` is evaluated.

Compare this to the treatment of array creation expressions, for which the out-of-memory condition is detected after evaluation of the dimension expressions ([§15.10.2](15.10.2.md)).
