@Override on a record component compiles but does not work

Vicente Romero vicente.romero at oracle.com
Mon Jul 19 19:54:41 UTC 2021


Hi Remi,

According to JLS 16 9.6.4.4:

...
The clause about a record class is due to the special meaning of 
@Override in a record
declaration. Namely, it can be used to specify that a method declaration 
is an accessor
method for a record component. Consider the following record declaration:

     record Roo(int x) {
         @Override
             public int x() {
                 return Math.abs(x);
             }
         }
     }

The use of @Override on the accessor method int x() ensures that if the 
record
component x is modified or removed, then the corresponding accessor 
method must be
modified or removed too.
...

So it is OK to annotate a record component with the @Override annotation 
without the need for a superinterface defining an override equivalent 
method to exist,

Thanks,
Vicente

On 7/19/21 12:49 PM, Remi Forax wrote:
> It does not seems that @Override correctly compiles on a record but the "override" check is not done.
>
> By example,
>    interface Named {
>      String name();
>    }
>    record Foo(@Override String name) implements Named { }
>
> actually compiles but
>    record Foo(String name, @Override String bar) implements Named { }
>
> also compiles ??
>
> It seems that the logic that checks if a method "overrides" another (in the general sense i.e replace another method) is not implemented if @Override is declared on a record component.
>
> regards,
> Rémi

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20210719/2c71abb4/attachment.htm>


More information about the compiler-dev mailing list