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).
The only restrictions on class names at the bytecode level are that they can't contain the characters [, . or ; and that they're at most 65535 bytes long. Among other things, this means that you can freely use reserved words, whitespace, special characters, Unicode, or even weird stuff like newlines.
You can theoretically even use null characters in a class name, but since it's impossible to have a null character in the filename, you can't include such a classfile in a jar. You might be able to create and load one dynamically though.
Here's an example of some of the things that you can do (written in Krakatau assembly):
; Entry point for the jar
.class Main
.super java/lang/Object
.method public static main : ([Ljava/lang/String;)V
.limit stack 10
.limit locals 10
invokestatic int hello ()V
invokestatic "-42" hello ()V
invokestatic "" hello ()V
invokestatic " some whitespace and \t tabs" hello ()V
invokestatic "new\nline" hello ()V
invokestatic 'name with "Quotes" in it' hello ()V
return
.end method
.end class
.class int
.super java/lang/Object
.method public static hello : ()V
.limit stack 2
.limit locals 0
getstatic java/lang/System out Ljava/io/PrintStream;
ldc "Hello from int"
invokevirtual java/io/PrintStream println (Ljava/lang/Object;)V
return
.end method
.end class
.class "-42"
.super java/lang/Object
.method public static hello : ()V
.limit stack 2
.limit locals 0
getstatic java/lang/System out Ljava/io/PrintStream;
ldc "Hello from -42"
invokevirtual java/io/PrintStream println (Ljava/lang/Object;)V
return
.end method
.end class
; Even the empty string can be a class name!
.class ""
.super java/lang/Object
.method public static hello : ()V
.limit stack 2
.limit locals 0
getstatic java/lang/System out Ljava/io/PrintStream;
ldc "Hello from "
invokevirtual java/io/PrintStream println (Ljava/lang/Object;)V
return
.end method
.end class
.class " some whitespace and \t tabs"
.super java/lang/Object
.method public static hello : ()V
.limit stack 2
.limit locals 0
getstatic java/lang/System out Ljava/io/PrintStream;
ldc "Hello from some whitespace and \t tabs"
invokevirtual java/io/PrintStream println (Ljava/lang/Object;)V
return
.end method
.end class
.class "new\nline"
.super java/lang/Object
.method public static hello : ()V
.limit stack 2
.limit locals 0
getstatic java/lang/System out Ljava/io/PrintStream;
ldc "Hello from new\nline"
invokevirtual java/io/PrintStream println (Ljava/lang/Object;)V
return
.end method
.end class
.class 'name with "Quotes" in it'
.super java/lang/Object
.method public static hello : ()V
.limit stack 2
.limit locals 0
getstatic java/lang/System out Ljava/io/PrintStream;
ldc "Hello from name with \"Quotes\" in it"
invokevirtual java/io/PrintStream println (Ljava/lang/Object;)V
return
.end method
.end class
Execution output:
Hello from int
Hello from -42
Hello from
Hello from some whitespace and tabs
Hello from new
line
Hello from name with "Quotes" in it
See Holger's answer for the exact quote of the rules from the JVM specification.
IMHO, this is a very bad idea. Reserved words are reserved for a reason, and doing this does decrease readability.
I also entirely agree with your second point. Naming a variable class, even if you could do it, would be just as bad as naming it tmp or a. What kind of class? A class of what? Names should be descriptive.
Python's Style Guide calls out this problem specifically, and suggests:
If your public attribute name collides with a reserved keyword, append a single trailing underscore to your attribute name. This is preferable to an abbreviation or corrupted spelling.
This seems like a pretty good general rule, assuming it doesn't conflict with the semantics of a particular language.
I read this link: https://stackoverflow.com/questions/1078908/what-is-the-difference-between-keyword-and-reserved-word#:~:text=Keywords%20have%20a%20special%20meaning,reserved%20words%20and%20vice%20versa.
I understand how reserved words are keywords and how some keywords cannot be reserved words, but I do not understand how some reserved words cannot be keywords? The answer to the post says how goto is not a keyword, but the java docs say how goto is a reserved word but also a keyword. Does this mean that reserved words are always keywords, or is there an instance/example in some language where a reserved word is not a keyword, would this mean that the StackOverflow post is wrong?
Certain language such as Python and C++ seem to use both of these terms interchangeably, so it depends on the language as well?
clazz has been used in Java in place of the reserved word "class" since JDK 1.0. "class" is what you want, but abbreviating or inserting junk ("a", "the", "_", etc) reduces clarity. clazz just says class. "International" English speakers (those reading both British and American English) are used to transposing 's' and 'z'.
Since Java has had disclosed source and a suitable culture right from the start, worthwhile Java code and tutorials pick up the same conventions. That's one of the great things about the Java ecosystem, which I think has been an important part of its success.
Because they cannot use the word they want to use which is class. It is reserved.