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

6.5.6.1: Shouldn't allow field references before 'this()'

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 25
    • specification

      Flexible Constructor Bodies (JEP 513) allows assignments to fields in an early construction context, with some restrictions. We didn't anticipate one extra necessary restriction: assignments should not happen before an alternate constructor invocation.

      This is a problem mainly because of future language evolution:

      - We'd like to support early reads of fields, implemented via proxy locals. But a proxy local won't be preserved across a constructor invocation, so the assigned value will be lost.

      - We'd like to support early references to fields with initializers, where initializers run "first" in the construction process; but the earliest initializers can be run is the start of a super-calling constructor, where they will overwrite whatever was written in a this-calling constructor.

      - We'd like to support user-defined default values for non-nullable types, but again these initial values can't be written until the start of a super-calling constructor, overwriting whatever was written in a this-calling constructor.

      The right model for users is that the prologue of a this-calling constructor exists at a uniquely sensitive point of construction—you can set up parameters to pass to another constructor, but you can't interact at all yet with the current object.

            gbierman Gavin Bierman
            dlsmith Dan Smith
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: