Java Compiled Classes with Dollar Signs
Java programmers often encounter a peculiar naming convention in their compiled class files. Some classes exhibit an odd pattern of their class name followed by a dollar sign and a number. For instance, you might see files like:
Delving into the Cause
Contrary to popular belief, the size of the class does not trigger this behavior. The answer lies in inner classes. When a class contains inner classes, the compiler generates separate class files for those inner classes. The file name follows the pattern:
OuterClass.class OuterClass$InnerClass.class
Anonymous Inner Classes
Anonymous inner classes, which are defined without a custom name, are assigned numbers. This explains the "Find$1.class" and similar file names.
Example
Consider the following code snippet:
public class OuterClass { class InnerClass { } Serializable anonymous = new Serializable() { }; }
The compiler will generate the following class files:
Anonymous Classes Considered?
The use of anonymous inner classes has been subject to debate. Some consider it a code smell, while others argue its appropriateness in certain scenarios. Ultimately, the decision to use them is based on specific design requirements.
The above is the detailed content of Why Do Java Compiled Classes Have Dollar Signs?. For more information, please follow other related articles on the PHP Chinese website!