views:

424

answers:

5
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 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(); 
               }
            }
         };
   }
}
+5  A: 

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

jitter
But there is no inner class in WelcomeApplet.java file.let me update with the file content.
Shore
Yes, there is. It is starting like this: return new ActionListener() { public void actionPerformed ...
Juha Syrjälä
There is an inner class in your applet the ActionListener you create in the makeURLActionListener()-Metod is an inline definition of a class. You say new ActionListener () followed by the class definition in { ... } where you implement actionPerformed method
jitter
+8  A: 

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.

Joachim Sauer
I've paste the source here,and there is no anonymous inner class
Shore
@Shore: Yes there is. Do you know what an anonymous inner class is?
Michael Myers
There is one anonymous class: new ActionListener() { ... } will create a anonymous subclass of ActionListener
Juha Syrjälä
What's the definition of "anonymous inner class"?
Shore
The link given explains what anonymous inner classes are. It's just a normal class that's not named... it's heavily used in Java Swing, so you'll find more information about it if you read Swing tutorials regarding event listeners, since you appear to starting on applets.
aberrant80
+2  A: 

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.

Kosi2801
But there is no such anonymous inner class in the source at all
Shore
Of course, in makeURLActionListener() you're generating an anonymous inner Class for the ActionListener. That's the origin :)
Kosi2801
+1  A: 

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.

jjnguy
What if it's not called?will '$1.class' be generated?
Shore
yes. that class is generated no matter what. however you will never have any instances of those objects.
jjnguy
A: 

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".

Roland Tepp