Java Reference
In-Depth Information
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 oc-
curs 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.
out-of-memory condition is detected after evaluation of the dimension expressions
15.9.5. Anonymous Class Declarations
An anonymous class declaration is automatically derived from a class instance creation ex-
pression by the Java compiler.