Home > Java > javaTutorial > body text

## What Are Dollar Signs in Compiled Java Classes and What Do They Mean?

Susan Sarandon
Release: 2024-10-26 09:18:02
Original
980 people have browsed it

## What Are Dollar Signs in Compiled Java Classes and What Do They Mean?

Understanding Dollar Signs in Compiled Java Classes

When inspecting JAR files generated from Eclipse, users occasionally encounter classes with dollar signs ($), followed by numbers. This phenomenon, particularly noticeable in larger classes, has prompted questions about whether it indicates multiple compiled versions of the class.

Explanation

The presence of dollar signs within class names signifies inner classes. These are additional classes defined within outer classes. The compiler assigns a unique number to each inner class to differentiate them.

For instance, the following code defines an outer class with two nested classes:

<code class="java">public class Find {
    private class InnerClass1 {}
    private class InnerClass2 {}
}</code>
Copy after login

Upon compilation, the following class files will be generated:

  • Find.class
  • Find$InnerClass1.class
  • Find$InnerClass2.class

Additionally, anonymous inner classes, which are classes that are declared and instantiated in a single statement, are also represented with dollar signs followed by numbers.

<code class="java">public class Main {
    public static void main(String[] args) {
        new Thread() { // Anonymous inner class
            // Override run() method
        }.start();
    }
}</code>
Copy after login

In this example, the anonymous inner class will result in a class file named Main$1.class.

Implication for Class Size

Contrary to the initial assumption, the size of the outer class does not determine whether inner classes will be generated. Inner classes are generated solely based on the presence of nested classes, regardless of the outer class's size.

The above is the detailed content of ## What Are Dollar Signs in Compiled Java Classes and What Do They Mean?. 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!