Yes, you can use reserved words. The words are only for the compiler. They do not appear in the generated byte code.
An example of using reserved Java words is in the JVM-based Scala language. Scala has different constructs and syntax than Java, but compiles to Java byte code, for running on a JVM.
This is legal Scala:
class `class`
This defines a class named class
with a no-arg constructor. Running javap
(a disassembler) on the compiled class.class
file shows
public class class {
public class();
}
Scala can do the same with any other Java reserved word.
class int
class `while`
class goto
They can also be used for method or field names.
As you suspected, you would not be able to use these classes from Java, except for reflection. You could use these from a similarly “customized” class file, e.g. from a class file generated by the Scala compiler.
In summary, this is a limitation of javac (the compiler), not java (the VM/runtime environment).