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

Compile time error was not generated when final field not definitely assigned at the declaration/constructor

XMLWordPrintable

      According to the spec,

      In a value class, a blank final instance variable must be definitely assigned after the argument list of an explicit superclass constructor invocation (8.8.7.1).

      Link: https://cr.openjdk.org/~dlsmith/jep401/jep401-20240624/specs/value-objects-jls.html#jls-8.3.1.2
       
                      Test Code:
                      value class ValueClass {
                        int f;
       
                       {
                              f = 7;
                       }
       
                       public int get() { return f; }
                    }
                      
                      The Valhalla java runtime throws the below error. In jdk23, normal class this final field was initialized successfully.
       
                      PS C:\SivaG\Official\JavaProject\src> java --enable-preview TestCls
      Exception in thread "main" java.lang.VerifyError: Illegal use of putfield on a strict field
      Exception Details:
                      Location:
                      ValueClass.<init>()V @7: putfield
                      Reason:
                      Error exists in the bytecode
                      Bytecode:
                       0000000: 2ab7 0001 2a10 07b5 0007 b1
       
                      at TestCls.main(TestCls.java:16)

            Unassigned Unassigned
            sgurusamy Sivakumar Gurusamy
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: