-
Bug
-
Resolution: Fixed
-
P4
-
6, 7, 8, 9
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-2185330 | 6-pool | Alex Buckley | P3 | Closed | Won't Fix |
JLS chapter "*14.10 The assert Statement*" says:
-----------------JLS---------------------
An assert statement causes the enclosing top level class (if it exists) to be initialized, if it has not already been initialized (§12.4.1).
Discussion
Note that an assertion that is enclosed by a top-level interface does not cause initialization. stmt312
Usually, the top level class enclosing an assertion will already be initialized. However, if the assertion is located within a static nested class, it may be that the initialization has nottaken place.
---------------------------------------
The following code is written to test assertion:
"An assert statement causes the enclosing top level class (if it exists) to be initialized".
In this test "assert" statement is located within a static nested class:
===============Code================
class Test01 {
static {
stmt31201.i = 1;
}
static class Test01_1 {
boolean test() {
boolean enabled = false;
assert enabled = true;
return enabled;
}
}
}
public class stmt31201 {
static int i = 0;
public static void main(String args[]) {
System.exit(run(args, System.out) + 95/*STATUS_TEMP*/);
}
public static int run(String argv[], PrintStream out) {
out.println("i = " + i);
Test01.Test01_1 obj = new Test01.Test01_1();
obj.test();
out.println("i = " + i);
if (i != 1) {
out.println("Top-level class hasn't been initialized");
return 2;
}
return 0;
}
}
===============Output============================
i = 0
i = 0
Top-level class hasn't been initialized
================================================
Hence the static initializer in Test_01 hasn't been executed.
It seems that realization contradicts the specification.
-----------------JLS---------------------
An assert statement causes the enclosing top level class (if it exists) to be initialized, if it has not already been initialized (§12.4.1).
Discussion
Note that an assertion that is enclosed by a top-level interface does not cause initialization. stmt312
Usually, the top level class enclosing an assertion will already be initialized. However, if the assertion is located within a static nested class, it may be that the initialization has nottaken place.
---------------------------------------
The following code is written to test assertion:
"An assert statement causes the enclosing top level class (if it exists) to be initialized".
In this test "assert" statement is located within a static nested class:
===============Code================
class Test01 {
static {
stmt31201.i = 1;
}
static class Test01_1 {
boolean test() {
boolean enabled = false;
assert enabled = true;
return enabled;
}
}
}
public class stmt31201 {
static int i = 0;
public static void main(String args[]) {
System.exit(run(args, System.out) + 95/*STATUS_TEMP*/);
}
public static int run(String argv[], PrintStream out) {
out.println("i = " + i);
Test01.Test01_1 obj = new Test01.Test01_1();
obj.test();
out.println("i = " + i);
if (i != 1) {
out.println("Top-level class hasn't been initialized");
return 2;
}
return 0;
}
}
===============Output============================
i = 0
i = 0
Top-level class hasn't been initialized
================================================
Hence the static initializer in Test_01 hasn't been executed.
It seems that realization contradicts the specification.
- backported by
-
JDK-2185330 assert must cause the enclosing top level class to be initialized?
-
- Closed
-
- relates to
-
JDK-4721843 are assertions sampled once per class or once per outermost class?
-
- Closed
-
-
JDK-8043189 12.4.1: Should asserts trigger initialization of outer classes?
-
- Closed
-