Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Java class files use reserved keywords as names?

I'm aware that Java-the-compilable-programming-language is not one and the same as Java-the-bytecode-format-for-JVM-execution. There are examples of things that are valid in the .class format but not in the .java source code, such as constructor-less classes and synthetic methods.

  1. If we hand-craft a .class file with a reserved Java language keyword (e.g. int, while) as the class, method, or field name, will the Java virtual machine accept it for loading?

  2. If the class is loaded, does it imply that the only way to access this class or member is through Java reflection, because the name is syntactically illegal in the Java programming language?

like image 929
Nayuki Avatar asked May 27 '15 19:05

Nayuki


People also ask

Can we use reserved keywords as identifiers in Java?

You cannot use keywords like int , for , class , etc as variable name (or identifiers) as they are part of the Java programming language syntax. Here's the complete list of all keywords in Java programming. Beside these keywords, you cannot also use true , false and null as identifiers. It is because they are literals.

Can you use reserved keywords as identifiers?

In a computer language, a reserved word (also known as a reserved identifier) is a word that cannot be used as an identifier, such as the name of a variable, function, or label – it is "reserved from use". This is a syntactic definition, and a reserved word may have no user-defined meaning.

Can we give any name to class in Java?

yes, you can choose any name for the file (. java). there is no matter to what are the name of classes in that file means that class names may be totaly different from the file name. you should compile the program with file name and you should run the program with the class name in which the main method exist.

Can class names have numbers Java?

Java class names usually begin with a capital letter. Java class names cannot begin with a number. if there are multiple words in the class name like "MyClassName" each word should begin with a capital letter.


2 Answers

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).

like image 139
Paul Draper Avatar answered Oct 19 '22 15:10

Paul Draper


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.

like image 32
Antimony Avatar answered Oct 19 '22 15:10

Antimony