Home > Java > javaTutorial > body text

Why Do Java Compiled Classes Have Dollar Signs?

Patricia Arquette
Release: 2024-10-26 18:54:29
Original
919 people have browsed it

Why Do Java Compiled Classes Have Dollar Signs?

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:

  • Find$1.class
  • Find$2.class
  • Find$3.class
  • Find.class

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
Copy after login

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() {
    };
}
Copy after login

The compiler will generate the following class files:

  • OuterClass.class
  • OuterClass$InnerClass.class
  • OuterClass$1.class

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!