CHAPTER 15: Expressions Previous
Previous
Java Language
Java Language
Index
Index
Next
Next

15.8 Class Instance Creation Expressions

15.8.1 Run-time Evaluation of Class Instance Creation Expressions , 15.8.2 Example: Evaluation Order and Out-of-Memory Detection

A class instance creation expression is used to create new objects that are instances of classes.


ClassInstanceCreationExpression:

	new ClassType ( ArgumentListopt )

ArgumentList:

	Expression

	ArgumentList , Expression

In a class instance creation expression, the ClassType must name a class that is not abstract . This class type is the type of the creation expression.

The arguments in the argument list, if any, are used to select a constructor declared in the body of the named class type, using the same matching rules as for method invocations (S15.11). As in method invocations, a compile-time method matching error results if there is no unique constructor that is both applicable to the provided arguments and the most specific of all the applicable constructors.


15.8.1 Run-time Evaluation of Class Instance Creation Expressions

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

First, 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 (S15.8.2).

The new object contains new instances of all the fields declared in the specified class type and all its superclasses. As each new field instance is created, it is initialized to its standard default value (S4.5.4).

Next, the argument list is 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 type is invoked. This results in invoking at least one constructor for each superclass of the class type. This process can be directed by explicit constructor invocation statements (S8.6) and is described in detail in S12.5.

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.


15.8.2 Example: 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) {
			System.out.println(e + ", " + (oldid==id));
		}
	}
}

prints:

java.lang.OutOfMemoryError: List, false

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

Compare this to the treatment of array creation expressions (S15.9), for which the out-of-memory condition is detected after evaluation of the dimension expressions (S15.9.3).

Top© 1996 Sun Microsystems, Inc. All rights reserved.