2

Here's a simple case:

    private final MouseAdapter mouse = new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            calculate();
        }
    };

It's a class level field, so calling it an Anonymous Class doesn't seem right. None of the other names or descriptions in the Oracle Tutorials page on Nested Classes seemed to fit either.

I'm guessing it's something along the lines of "Single Use Object" but I'm having a hard time even describing it without saying something like "Class Level Named Anonymous Class"


For those not familiar with Java and AWT, I'm making an instance of a class that has no-operation methods to implement an interface for listening to mouse actions. I want an actual instance so I can add it as multiple types of listeners (wheel, motion, and click) but use the same object for control. The question itself is not AWT specific though.

Eben
  • 422
  • 2
  • 11

3 Answers3

4

Let's split it in pieces

  • private final MouseAdapter mouse is called a class member, which type (MouseAdapter) denotes that the member can refer to instances and/or sub-classes of MouseAdapter.

  • new MouseAdapter() { ... } is called an anonymous implementation of the MouseAdapter interface/abstract class.

So, to summarize: the class member mouse holds a reference to an anonymous implementation of the MouseAdapter interface/abstract class.

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
  • Nice answer. For terminology's sake, you agree that an "anonymous implementation of a class" is the same as an "anonymous class", right? – Erwin Bolwidt Oct 17 '14 at 06:58
  • Correct. They're the same, but with "anonymous implementation of a class" you can point which exact class is implemented. – Konstantin Yovkov Oct 17 '14 at 07:00
2

It is an instance of an anonymous class, there's no need to find a new name for this.

From Oracle docs :

Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local class only once.

It does not say the instance is to be used only once, but only the class, so there is no contradiction with your case.

Dici
  • 25,226
  • 7
  • 41
  • 82
  • Perfect. I've never made an anonymous class outside of a method before, so it felt super strange in Java, but normal in JS. I'd missed that detail in the docs. – Eben Oct 17 '14 at 06:57
  • 1
    I picked this as the answer because it very directly answers my question and nicely points to something I'd looked at but not read quite correctly enough. @kocko's answer is also very excellent and I wish I could accept two answers since you both deserve it – Eben Oct 17 '14 at 07:10
1

Agree with kocko's answer but want to add one thing which is ,

Anonymous classes are expressions, which means that you define the class in another expression.

So no matter where you declare it,it will remain anonymous class and no need to name your declaration differently. :)

akash
  • 22,664
  • 11
  • 59
  • 87