Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6394986

assert must cause the enclosing top level class to be initialized?

    Details

    • Subcomponent:
    • Introduced In Version:
    • Resolved In Build:
      rc
    • CPU:
      generic
    • OS:
      generic
    • Verification:
      Verified

      Backports

        Description

        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.

          Attachments

            Issue Links

              Activity

                People

                • Assignee:
                  abuckley Alex Buckley
                  Reporter:
                  ynovozhi Yulia Novozhilova (Inactive)
                • Votes:
                  0 Vote for this issue
                  Watchers:
                  2 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved:
                    Imported:
                    Indexed: