related to compile time error in record constructor

Brian Goetz brian.goetz at oracle.com
Wed Oct 7 11:18:54 UTC 2020



> This error seems to be unnecessary, could have been a warning instead.

The error is correct, because the variable this.low really is not 
initialized yet!   You say the call to m() "works", but when you call 
it, it will see this.low == 0.

Arguably, `this` should be considered DU for the entirety of the compact 
constructor.

> In the constructor of record access to instance variable before
> explicit initialization, giving error, whereas same access is
> available by invoking a method.
>
> public record TestRecord(int low, int high) {
>      public TestRecord {
>          System.out.println(low);
> //        System.out.println(this.low); // error, variable not initialized
>          m1(); // but this works, why?
>      }
>      public void m1() {
>          System.out.println(this.low);
>      }
>      public static void main(String[] args) {
>          TestRecord r1 = new TestRecord(7,12);
>      }
> }
>
>



More information about the amber-dev mailing list