When you refer this.list it implies property of the current object/class, and in records all the props are final
it's similar to
final String s = "ABC";
now if you try
s = "PQR";
it will give you the same error cause 's' is final
public record A(List<String> list){
public A{
this.list=new ArrayList<>(list);
}
}
although you can do this
public record A(List<String> list){
public A(List<String> list){
this.list=new ArrayList<>(list);
}
}
records' all the properties are final, you cannot reassign value to record that's one reason to use records
if you want to reassign values use classes
reason why you are not getting error in second part is because you're creating compact constructor, What you should really do is
public record A(List<String> list){
public A(List<String> list){
this.list=new ArrayList<>(list);
}
public A{
if(!Objects.nonNull(list){
throw new IllegalArgumentException("List must not be empty")
}
}
}
in other words, when you initialize new record, it will simply set that parameter values to it's properties and when you try to reassign using this keyword it will definitely throw error since it's properties are final
and for second code block you are just assigning value to your parameter 'list' you are not changing your class properties
I hope this answers your question.
for more details go here