The OpenJDK official website announced a new JEP draft, which no longer requires super() or this() to appear as the first statement in the constructor.

The draft’s current goal is to change the Java Language Specification, with corresponding changes to the Java compiler, so that:

  • super()orthis()no longer has to appear as the first statement in a constructor
  • preserve the existing safety and initialization guarantees provided to the constructor
  • Existing programs continue to compile and run as before

According to the current Java language specification, the first statement of the constructor must callsuper()orthis(). But the Java Virtual Machine actually supports more flexibility:

  • this()orsuper()Multiple invocations of may appear in the constructor,As long as there is a single call on any code path
  • Arbitrary code can appear inthis()/super()Before,As long as that code doesn’t refer to the instance being constructed
  • butthis()/super()calls may not appear intry { }block

In fact, JLS is subject to many unnecessary restrictions. And this inconsistency is a historical artifact: the original JVM specification was also more restrictive, which led to initialization problems for compiler-generated synthetic fields – in order to support new language features such as inner classes and captured free variables. Now the JVM specification was relaxed to accommodate the compiler, but this new flexibility never made it back to the language level.

See https://openjdk.org/jeps/8300786 for details.

#Javas #latest #JEP #draft #longer #requires #line #constructor #super #News Fast Delivery

Leave a Comment

Your email address will not be published. Required fields are marked *