1

I think I understand the basics of Anonymous classes but I'd like to clarify something. when I have a syntax such as this

class A
{
       class AnonymousClass1 Implements ActionListener{}
}
class A
{
     public A()
     {
        JButton btn = new JButton();

       btn.addActionListener( new ActionListener(){} );
     }
}

If the anonymous class is actually an inner class of class A, as in the first example: in theory, is the semantics right?

What happens exactly? I think when the java file is compiled, a .class file is created for the anonymous class so it can be referenced (but I couldn't find it). When an object of A is instantiated it creates a button object, btn then calls the addActionListener() method which actually passes something like this btn.addActionListener(new AnonymousClassOne()) AnonymousClassOne a generic name given by the compiler.

If not what happens? Thanks.

Lews Therin
  • 10,907
  • 4
  • 48
  • 72

2 Answers2

4

Anonymous classes can be recognized by the dollar sign and a number after it - Class$1.class. These classes are just for your own convenience. Imagine this:

class SaveButtonListener implements ActionListener {
  ...
}

class OpenButtonListener implements ActionListener {
  ...
}

This is very tedious. So you can create the implementation right away with an anonymous class. The compiler gives the name prepending the dollar sign and some identifier after it.

What happens behind the scenes is that Java creates a new inner class with an auto-generated name.

Feel free to ask questions if you find my explanation messy. I am tired now.

Community
  • 1
  • 1
Petar Minchev
  • 46,889
  • 11
  • 103
  • 119
  • lol I saw it! This is brilliant. It wasn't in Eclipse I was looking at the wrong place. Nice one thanks. It is funny it actually uses the classname$Digit as the anonymous class name. Very cool! – Lews Therin Oct 15 '11 at 15:14
  • Why don't you hang out in the Java room? We're always alone there lol – Lews Therin Oct 15 '11 at 15:18
  • @Lews Therin - I guess I am not used to using the chat feature of StackOverflow. Maybe I will start using it more often:) LOL, I used "use" three times. – Petar Minchev Oct 15 '11 at 15:20
  • Lol I noticed that xD, nice one :) You should it'd be nice :D One more question, I tried to use the name of the .class file I found in the folder. TextField$1 tField = new TextField$1() lol it gave me a compilation error. Why? Does it check only the ClassName.java file to see if it exists – Lews Therin Oct 15 '11 at 15:25
  • It is up to the compiler what name it will generate. You don't know it. Also when you use anonymous class, you are doing it because you don't care about the name. – Petar Minchev Oct 15 '11 at 15:30
  • That's true. I just thought of I could go into the folder look for the name and try to use it in the .java file it'd work lmao. Big mistake! – Lews Therin Oct 15 '11 at 15:32
2
class A
{
    public A()
    {
        JButton btn = new JButton();
        btn.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                // ...
            }
        });
    }
}

is more or less rewritten by the compiler as

class A
{
    private class SomeCuteName implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            // ...
        }
    }

    public A()
    {
        JButton btn = new JButton();
        btn.addActionListener(new SomeCuteName());
    }
}
fredoverflow
  • 256,549
  • 94
  • 388
  • 662