The problem we’re trying to prevent is clashes between the names of classes/methods and contextual keywords. Clashes can occur when (1.) naming a class/method, or (2.) when invoking.
Change problematic names for classes and methods.
class Foo {
...
// This can clash with the contextual keyword "yield"
void yield() {
...
}
}
Another example:
// This can clash with Java modules (JPMS)
static class module {
...
}
In recent versions of Java, yield
is a restricted identifier:
class T {
void yield() {}
{
yield();
}
}
$ javac --release 20 T.java
T.java:3: error: invalid use of a restricted identifier 'yield'
yield();
^
(to invoke a method called yield, qualify the yield with a receiver or type name)
1 error
To invoke existing methods called yield
, use qualified names:
class T {
void yield() {}
{
this.yield();
}
}
class T {
static void yield() {}
{
T.yield();
}
}
class T {
void yield() {}
class I {
{
T.this.yield();
}
}
}
Suppress false positives by adding the suppression annotation @SuppressWarnings("NamedLikeContextualKeyword")
to the enclosing element.