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

[lworld] instance variable initializer erroneously allows to refer to instance variables, methods, this, or super

XMLWordPrintable

      According to the spec,

      If the declarator is for an instance variable (that is, a field that is not static) of a value class (8.1.1.5), then the initializer is an early instance variable initializer and occurs in an early construction context (8.8.7.1); the initializer may refer to any class variable of the class, even one whose declaration occurs to the right of the initializer. At run time, the initializer is evaluated and the assignment performed each time an instance of the class is created, at the start of the class's construction process (12.5).

      The following cases should therefore fail with a compilation error:

      value class C {

          int b = this.a;

          int a;

          C() {
              a = 5;
          }
      }
       
          public static void main(String[] args) {
              C c = new C();
              System.out.println(c.b);
          }
       
       
      Exception in thread "main" java.lang.VerifyError: Bad type on operand stack
      Exception Details:
        Location:
          C.<init>()V @7: getfield
        Reason:
          Type uninitializedThis (current frame, stack[1]) is not assignable to 'C'
        Current Frame:
          bci: @7
          flags: { flagThisUninit }
          locals: { uninitializedThis }
          stack: { uninitializedThis, uninitializedThis }
        Bytecode:
          0000000: 2a08 b500 012a 2ab4 0001 b500 072a b700
          0000010: 0ab1
       
                      at Main.main(Main.java:3)


      Case 2: instance variable declarator refers to another variable via this, no constructor

      value class C {
          int a = 5;
          int b = this.a;

      }

          public static void main(String[] args) {
              C c = new C();
              System.out.println(c.b);
          }
      Exception in thread "main" java.lang.VerifyError: Bad type on operand stack
      Exception Details:
        Location:
          C.<init>()V @7: invokestatic
        Reason:
          Type uninitializedThis (current frame, stack[1]) is not assignable to 'java/lang/Object'
        Current Frame:
          bci: @7
          flags: { flagThisUninit }
          locals: { uninitializedThis }
          stack: { uninitializedThis, uninitializedThis }
        Bytecode:
          0000000: 2a08 b500 012a 2ab8 0007 5708 b500 0d2a
          0000010: b700 10b1
       
                      at Main.main(Main.java:3)
       
      Case 3: instance variable declarator refers to another instance variable.


      value class C {
          int b = a;
          int a = 5;

      }

          public static void main(String[] args) {
              C c = new C();
              System.out.println(c.b);
          }

       
      Compilation succeeds and and the result will print 5.

            vromero Vicente Arturo Romero Zaldivar
            eananeva Ella Ananeva
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: