Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the $1 in class file names?

C:\Program Files\Java\jdk1.6.0_05\CoreJava\v1\v1ch2\WelcomeApplet>dir  Volume in drive C has no label.  Volume Serial Number is 2041-64E7   Directory of 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 File(s)          1,984 bytes                2 Dir(s)   2,557,210,624 bytes free  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  Volume in drive C has no label.  Volume Serial Number is 2041-64E7   Directory of 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 File(s)          4,338 bytes                2 Dir(s)   2,557,202,432 bytes free  C:\Program Files\Java\jdk1.6.0_05\CoreJava\v1\v1ch2\WelcomeApplet> 

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();                 }             }          };    } } 
like image 410
omg Avatar asked Jul 02 '09 15:07

omg


People also ask

What is $1 class file in Java?

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 { } }

What is a code in .class file called?

A Java class file is a file (with the . class filename extension) containing Java bytecode that can be executed on the Java Virtual Machine (JVM). A Java class file is usually produced by a Java compiler from Java programming language source files ( .

What's inside a .class file?

A CLASS file is a compiled . JAVA file created by the Java compiler. It contains bytecode, which is binary program code that is executable when run by a Java Virtual Machine (JVM). CLASS files are commonly bundled into .

What is class file in jar?

To update classes in JAR files, use the jar tool. Java class files are stream files that are produced when a source file is compiled by the Java compiler. The class file contains tables that describe each field and method of the class.


2 Answers

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.

like image 99
Joachim Sauer Avatar answered Oct 14 '22 11:10

Joachim Sauer


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

like image 26
jitter Avatar answered Oct 14 '22 12:10

jitter