related to compile time error in record constructor
Remi Forax
forax at univ-mlv.fr
Wed Oct 7 12:56:39 UTC 2020
----- Mail original -----
> De: "Brian Goetz" <brian.goetz at oracle.com>
> À: "Pravin Jain" <pravin at zensoftech.co.in>, "amber-dev" <amber-dev at openjdk.java.net>
> Envoyé: Mercredi 7 Octobre 2020 13:18:54
> Objet: Re: related to compile time error in record constructor
>> 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.
+1 to make 'this' DU inside the compact constructor.
Rémi
>
>> 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