Home >Java >javaTutorial >Why do Java compiled classes contain names like \'Find$1.class\'?

Why do Java compiled classes contain names like \'Find$1.class\'?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-26 00:45:27990browse

Why do Java compiled classes contain names like

Denoting Inner Classes in Java Compiled Classes

In Java, when exporting an application to a JAR file, certain classes may contain the class name followed by a dollar sign and a number, such as Find$1.class. This occurs because Java compiles inner classes, including anonymous inner classes, separately from the main class.

Inner classes, defined within another class, enable object-oriented programming concepts like encapsulation and code reusability. They are compiler-generated and their class files have the following naming convention:

  • Named inner classes: OuterClass$InnerClass.class
  • Anonymous inner classes: OuterClass$1.class (where 1 represents the anonymous class)

For instance, in the following code:

<code class="java">public class OuterClass {
    public class InnerClass {
        // ...
    }

    public static void main(String[] args) {
        OuterClass.InnerClass inner = new InnerClass();
    }
}</code>

Compilation will generate the following class files:

  • OuterClass.class
  • OuterClass$InnerClass.class

The dollar sign in class file names serves as a separator to differentiate between outer and inner classes. It indicates that the inner class belongs to the specified outer class.

The above is the detailed content of Why do Java compiled classes contain names like \'Find$1.class\'?. 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