Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java, anonymous inner class definition

I've seen a couple of examples similar to this in Java, and am hoping someone can explain what is happening. It seems like a new class can be defined inline, which seems really weird to me.

The first printout line is expected, since it is simply the toString. However the 2nd seems like the function can be overriden inline.

Is there a technical term for this?
Or any documentation which goes into more depth?

If I have the following code:

public class Apple {
    public String toString() {
        return "original apple";
    }
}

public class Driver {
    public static void main(String[] args) {
        System.out.println("first: " + new Apple());
        System.out.println("second: " + 
            new Apple() {
                public String toString() {
                    return "modified apple";
                }
            }
        );
    }
}

The code outputs:

first: original apple
second: modified apple
like image 328
lots_of_questions Avatar asked Jan 18 '12 16:01

lots_of_questions


People also ask

What is the difference between inner class and anonymous class in Java?

A local inner class consists of a class declared within a method, whereas an anonymous class is declared when an instance is created. So the anonymous class is created on the fly or during program execution.

Which is true about an anonymous inner class in Java?

Ans: C Explanation: Option C is correct because the syntax of an anonymous inner class allows for only one named type after the new, and that type must be either a single interface (in which case the anonymous class implements that one interface) or a single class (in which case the anonymous...

Which of the following options defines an anonymous inner class?

D. Explanation: D is correct. It defines an anonymous inner class instance, which also means it creates an instance of that new anonymous class at the same time.

What is the meaning of Anonymous in Java?

In Java, a class can contain another class known as nested class. It's possible to create a nested class without giving any name. A nested class that doesn't have any name is known as an anonymous class. An anonymous class must be defined inside another class. Hence, it is also known as an anonymous inner class.


2 Answers

It is an anonymous inner class. You can find some more information about it at the Java documentation link for Inner Classes. EDIT I am adding a better link describing anonymous inner classes, as the Java documentation leaves something to be desired. /EDIT

Most people will use Anonymous inner classes to define listeners on the fly. Consider this scenario:

I have a Button, and when I click it I want it to display something to the console. But I do not want to have to create a new class in a different file, and I don't want to have to define an inner class later in this file, I want the logic to be immediately available right here.

class Example {
    Button button = new SomeButton();

    public void example() {
        button.setOnClickListener(new OnClickListener() {
            public void onClick(SomeClickEvent clickEvent) {
                System.out.println("A click happened at " + clickEvent.getClickTime());
            }
        });
    }

    interface OnClickListener {
        void onClick(SomeClickEvent clickEvent);
    }

    interface Button {
        void setOnClickListener(OnClickListener ocl);
    }
}

The example is somewhat contrived and obviously not complete, but I think it gets the idea across.

like image 195
nicholas.hauschild Avatar answered Oct 03 '22 00:10

nicholas.hauschild


What is happening in your code is that you are implicitly subclassing Apple with an anonymous inner class and overriding its toString() method.

like image 45
Unai Vivi Avatar answered Oct 02 '22 23:10

Unai Vivi