Class ObjectUsedBeforeConstructedOrError


final class ObjectUsedBeforeConstructedOrError extends TypedSymbolAccess
An object cannot be used before it is fully constructed (E08061).

Construction runs the super constructor, then this type's property initialisers in declaration order, then the constructor body. Until every property has a value, calling a method on the object - or handing out 'this' - can reach a property that is still unset, and it failed with a NullPointerException at run time (front-end audit FE32). This is the two-phase rule: the object may not be used until it is complete.

  • A property initialiser runs before the constructor body, so the object is never complete there: no method call on it (even an inherited one - an inherited method can call back into one this type overrides) and no use of 'this' as a value.
  • A constructor may use the object once every property is set on the current path (the flow tracking E08060 already does) - counting what a 'this(...)' call set, which is only what the constructor it delegates to sets (DelegatedConstructorInitialisation): a private one may set nothing. Never inside the arguments of 'this(...)' or 'super(...)': nothing has been constructed yet.
  • In an open or abstract type, a constructor can never call a method a subtype may override, nor hand out 'this': the subtype's own properties are only set after this constructor returns.

Reading and writing a property (this.count) is not a use of the object: E08060 checks each property.

  • Constructor Details

  • Method Details

    • acceptCall

      void acceptCall(EK9Parser.CallContext ctx)
      A method called with no receiver - on the object itself - such as twice().
    • acceptPrimaryReference

      void acceptPrimaryReference(EK9Parser.PrimaryReferenceContext ctx)
      A use of 'this' or 'super': a call through it, or 'this' as a value. Reaching a property is not a use.