Home > Java > javaTutorial > body text

Why Do Compiled Java Classes Have Dollar Signs?

Mary-Kate Olsen
Release: 2024-10-25 10:35:31
Original
1034 people have browsed it

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

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!

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!