2

So, when looking into lambda expressions and using them to substitute anonymous inner classes for EventHandlers in Java, I came across a few anonymous inner classes that made me stop and think. For instance, when writing an anonymous inner class for something that normally implements ActionListener we write

myJButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e){
        //DO SOMETHING
    }
});

My confusion with this is, ActionListener is an interface so I thought it'd be necessary to do something like...

myJButton.addActionListener(new myButtonListener implements ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e){
        //DO SOMETHING
    }
});

But this doesn't even compile. I guess the reason I though this is obviously if instead we use a private inner class, we use

private MyButtonListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        //DO SOMETHING
    }
}
myJButton.addActionListener(new MyButtonListener());

So my questions are:

1) Why are we able to create an anonymous inner class directly from an interface rather than having to create one through a class that implements the interface?

2) Why am I unable to create an anonymous inner class that implements ActionListener instead of directly from it as I show in my second code snippet?

1
  • 4
    What do you think anonymous means here? Commented Oct 27, 2014 at 5:30

1 Answer 1

2

1) Why are we able to create an anonymous inner class directly from an interface rather than having to create one through a class that implements the interface?

2) Why am I unable to create an anonymous inner class that implements ActionListener instead of directly from it as I show in my second code snippet?

When you create a class using implements XXXX, you are defining a class(inner or non-inner), and you will have to give it a name, sure we can do that and this is what we often do . While anonymous inner class dose not have a name, and it is more like an expression.


I copy this from http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html

And I think this will help you to understand what anonymous class is.

An anonymous class is an expression. They are like local classes except that they do not have a name

. The syntax of an anonymous class expression is like the invocation of a constructor, except that there is a class definition contained in a block of code.

Consider the instantiation of the frenchGreeting object:

    HelloWorld frenchGreeting = new HelloWorld() {
        String name = "tout le monde";
        public void greet() {
            greetSomeone("tout le monde");
        }
        public void greetSomeone(String someone) {
            name = someone;
            System.out.println("Salut " + name);
        }
    };

The anonymous class expression consists of the following:

  • The new operator

  • The name of an interface to implement or a class to extend. In this example, the anonymous class is implementing the interface HelloWorld.

  • Parentheses that contain the arguments to a constructor, just like a normal class instance creation expression. Note: When you implement an interface, there is no constructor, so you use an empty pair of parentheses, as in this example.

  • A body, which is a class declaration body. More specifically, in the body, method declarations are allowed but statements are not.

Because an anonymous class definition is an expression, it must be part of a statement. In this example, the anonymous class expression is part of the statement that instantiates the frenchGreeting object. (This explains why there is a semicolon after the closing brace.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.