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

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

Susan Sarandon
Susan SarandonOriginal
2024-10-26 09:18:02962browse

## 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>

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>

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!

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