Home  >  Article  >  Java  >  Why Do Compiled Java Classes Have Dollar Signs?

Why Do Compiled Java Classes Have Dollar Signs?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-25 10:35:31961browse

Why Do Compiled Java Classes Have Dollar Signs?

Java Compiled Classes with Dollar Signs

When using Eclipse to export an application as a JAR file, some classes may contain a dollar sign and a number. This can be confusing, particularly for classes that are relatively large.

Explanation:

These classes are related to inner classes, which are classes declared within another class. When an inner class is compiled, it results in a separate class file named with the outer class name followed by a dollar sign and a number. For example, an inner class named Child within a class named Parent would result in a class file named Parent$Child.class.

Anonymous inner classes, which are declared without a name, are assigned a number instead. This explains the presence of class files like Find$1.class in the JAR file.

The size of the Java code does not influence the creation of multiple class files. Inner classes are created separately from the main class to maintain the modularity and organization of the code.

Example:

Consider the following code:

<code class="java">public class OuterClass {
    class InnerClass {
    }
    
    public static void main(String[] args) {
        // Create an instance of the inner class
        InnerClass inner = new InnerClass();
    }
}</code>

When compiled, this code will result in two class files: OuterClass.class and OuterClass$InnerClass.class. The main method will reference the InnerClass from the outer class.

The above is the detailed content of Why Do Compiled Java Classes Have Dollar Signs?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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