about bug: 8020216: Compile time error isn't shown when final class variable is not assigned

John Rose john.r.rose at oracle.com
Wed Aug 7 19:05:13 PDT 2013


On Aug 1, 2013, at 10:57 AM, Vicente-Arturo Romero-Zaldivar <vicente.romero at oracle.com> wrote:

> Is this what we want?

No.

For blank-final instance variables, the definite assignment is only required at *normal* completion of the constructor.  Abnormal completion (such as the throw in your example) *must* be allowed to happen when blank-finals are still unassigned.  Otherwise this code would fail:

class HasBlankF {
  final int hc;
  HasBlankF(Object x) {
    hc = x.hashCode();
  }
}

It would fail because there is a potential throw that works like this:

class HasBlankF {
  final int hc;
  HasBlankF(Object x) {
    if (x == null)  throw new NullPointerException();
    hc = x.hashCode();
  }
}

I added a comment to this effect on the bug report.

The same point applies for abnormal completion of <clinit> methods (static initializers).  Blank finals which are statics must be definitely assigned at normal termination of the last static initializer.  If a static initializer terminates abnormally, the class is not available to its users, and some of its blank finals may validly remain forever uninitialized.

— John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20130807/b5dad15b/attachment.html 


More information about the compiler-dev mailing list