UFO ET IT

클래스 파일 이름의 $ 1은 무엇입니까?

ufoet 2020. 12. 25. 00:16
반응형

클래스 파일 이름의 $ 1은 무엇입니까?


C : \ Program Files \ Java \ jdk1.6.0_05 \ CoreJava \ v1 \ v1ch2 \ WelcomeApplet> dir
 C 드라이브의 볼륨에는 레이블이 없습니다.
 볼륨 일련 번호는 2041-64E7입니다.

 C : \ Program Files \ Java \ jdk1.6.0_05 \ CoreJava \ v1 \ v1ch2 \ WelcomeApplet의 디렉토리

2009-07-02 23:54
2009-07-02 23:54 ..
2004-09-06 14:57 582 WelcomeApplet.html
2004-09-06 15:04 1,402 WelcomeApplet.java
               2 개 파일 1,984 바이트
               2 Dir (s) 2,557,210,624 바이트 사용 가능

C : \ Program Files \ Java \ jdk1.6.0_05 \ CoreJava \ v1 \ v1ch2 \ WelcomeApplet> javac WelcomeApplet.java

C : \ Program Files \ Java \ jdk1.6.0_05 \ CoreJava \ v1 \ v1ch2 \ WelcomeApplet> dir
 C 드라이브의 볼륨에는 레이블이 없습니다.
 볼륨 일련 번호는 2041-64E7입니다.

 C : \ Program Files \ Java \ jdk1.6.0_05 \ CoreJava \ v1 \ v1ch2 \ WelcomeApplet의 디렉토리

2009-07-02 23:54
2009-07-02 23:54 ..
2009-07-02 23:54 975 WelcomeApplet $ 1.class
2009-07-02 23:54 1,379 WelcomeApplet.class
2004-09-06 14:57 582 WelcomeApplet.html
2004-09-06 15:04 1,402 WelcomeApplet.java
               4 개 파일 4,338 바이트
               2 Dir (s) 2,557,202,432 바이트 사용 가능

C : \ Program Files \ Java \ jdk1.6.0_05 \ CoreJava \ v1 \ v1ch2 \ WelcomeApplet>

다음은 해당 Java 파일의 내용입니다.

/**
   @version 1.21 2002-06-19
   @author Cay Horstmann
*/

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

public class WelcomeApplet extends JApplet
{
   public void init()
   {
      setLayout(new BorderLayout());

      JLabel label = new JLabel(getParameter("greeting"), SwingConstants.CENTER);
      label.setFont(new Font("Serif", Font.BOLD, 18));
      add(label, BorderLayout.CENTER);

      JPanel panel = new JPanel();

      JButton cayButton = new JButton("Cay Horstmann");
      cayButton.addActionListener(makeURLActionListener(
         "http://www.horstmann.com"));
      panel.add(cayButton);

      JButton garyButton = new JButton("Gary Cornell");
      garyButton.addActionListener(makeURLActionListener(
         "mailto:gary@thecornells.com"));
      panel.add(garyButton);

      add(panel, BorderLayout.SOUTH);
   }

   private ActionListener makeURLActionListener(final String u)
   {
      return new
         ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               try
               {
                  getAppletContext().showDocument(new URL(u));
               }
               catch(MalformedURLException e) 
               { 
                  e.printStackTrace(); 
               }
            }
         };
   }
}

이것들은 익명의 내부 클래스.class 를 보유 하는 파일입니다 .

귀하의 예제 WelcomeApplet.java에는 최상위 클래스 (라고 함 WelcomeApplet)와 익명 내부 클래스가 포함되어 있습니다 WelcomeApplet$1.class.

익명 내부 클래스를 보유하는 파일의 정확한 이름은 표준화되지 않았으며 다를 수 있습니다. 그러나 실제로 여기에 설명 된 것 이외의 다른 계획은 아직 보지 못했습니다.

에 대한 값별 본문 enum 은 익명의 내부 클래스이기도합니다 .

The optional class body of an enum constant implicitly defines an anonymous class declaration (§15.9.5) that extends the immediately enclosing enum type.


The $1 are anonymous inner classes you defined in your WelcomeApplet.java file.

e.g. compiling

public class Run {
    public static void main(String[] args) {
        System.out.println(new Object() {
            public String toString() {
                return "77";
            }
        });
    }
    private class innerNamed {
    }
}

would result in Run.class, Run$1.class and Run$innerNamed.class being generated


These are generated from the inner and static nested classes in the WelcomeApplet.java file by the java compiler.

See also this similar question and answer.


It is from this 'line' of code:

return new
    ActionListener()
    {
        public void actionPerformed(ActionEvent event)
        {
            try
            {
                getAppletContext().showDocument(new URL(u));
            }
            catch(MalformedURLException e) 
            { 
                e.printStackTrace(); 
            }
        }
    };

The way you are declaring the ActionListener you are making an instance of the anonymous inner class each time that method is called.

Even if the method is not called, the above line still gets compiled into an anonymous inner class no matter what.


The WelcomeApplet$1.class file is generated for an anonymous class in the WelcomeApplet.java source (the anonymous class is generated in the method call makeURLActionListener by calling new new ActionListener() {...})

To explain more clearly, the anonymous classes are generated at compile time any time you have an instantiation of a concrete named class that overrides some or all of the behavior of the concrete class (or interface) inline like this:

class HelloInternalClass {
  public static final void main(String[] args) {
    // print in another thread
    new Thread(new Runnable() {
      public void run() {
        System.out.println("Printed from another thread");
      }
    }).start();
  }
}

In the above sample code, the javac compiler would generate 2 class files just like in your example: HelloInternalClass.class and HelloInternalClass$1.class.

The anonymous class in this instance would be a subclass of Runnable and would be compiled into HelloInternalClass$1.class. Incidentally, if you would ask a class name from the runnable instance in the above sample (by calling getClass().getName()) you would find that it thinks of itself as "HelloInternalClass$1".


Create:

public class A {
    public static void main(String[] args) {
        X x=new X();
        X x2=new X(){   
        };
        Class<? extends Object>c2=x2.getClass();
        Class<? extends Object>s2=x2.getClass().getSuperclass();

        boolean b=false;
    }
    private static class X{     
    }
}

It is hard to see from the code (new X{}() would have been better than new X(){}), but x2 is an instance of a subclass of A$X. This subclass is A$1.

ReferenceURL : https://stackoverflow.com/questions/1075207/what-is-the-1-in-class-file-names

반응형