Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
104 views
in Technique[技术] by (71.8m points)

java - What is the $1 in class file names?

C:Program FilesJavajdk1.6.0_05CoreJavav1v1ch2WelcomeApplet>dir
 Volume in drive C has no label.
 Volume Serial Number is 2041-64E7

 Directory of C:Program FilesJavajdk1.6.0_05CoreJavav1v1ch2WelcomeApplet

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 File(s)          1,984 bytes
               2 Dir(s)   2,557,210,624 bytes free

C:Program FilesJavajdk1.6.0_05CoreJavav1v1ch2WelcomeApplet>javac WelcomeApplet.java

C:Program FilesJavajdk1.6.0_05CoreJavav1v1ch2WelcomeApplet>dir
 Volume in drive C has no label.
 Volume Serial Number is 2041-64E7

 Directory of C:Program FilesJavajdk1.6.0_05CoreJavav1v1ch2WelcomeApplet

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 File(s)          4,338 bytes
               2 Dir(s)   2,557,202,432 bytes free

C:Program FilesJavajdk1.6.0_05CoreJavav1v1ch2WelcomeApplet>

Here is the content of that Java file:

/**
   @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:[email protected]"));
      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(); 
               }
            }
         };
   }
}
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Those are the .class files that hold the anonymous inner classes.

In your example WelcomeApplet.java contains a top-level class (called WelcomeApplet) and an anonymous inner class, which will be stored in WelcomeApplet$1.class.

Note that the exact name of the files holding anonymous inner classes is not standardized and might vary. But in practice I've yet to see any other scheme than the one described here.

Value-specific bodies for an enum are also anonymous inner classes:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...