Q:

Which is more efficient ways to use listener : using Anonymous class or implementing listner

class Rocket {

    public Rocket() {}

    public interface RocketListener {
        void RocketLaunched();
    }

    RocketListener listener;
    public void setRocketListener(RocketListener listener) {
        this.listener = listener;
    }

    //When rocket get launched.
    listener.RocketLaunched();
}

//first case : when implementing listner
class MyClass implements RocketListener {

    Rocket rocket = new Rocket();
    rocket.setRocketListener(this);

    @Override
    public void RocketLaunched() {
        //when rockect get launched. it get called
    }
}

//second case : when using anonymous class for listner
class MyClass {

    Rocket rocket = new Rocket();
    rocket.setRocketListener(new RocketListener() {

        @Override
        public void RocketLaunched() {
            //when rockect get launched. it get called
        }
    });
}  

In first case : When MyClass implements an Interface, then the parameter send in setOnClickListener() is the instance of class.
In second case : The parameter send in setOnClickListener() is the anonymous class.  
Question 1 : In second case, did the anonymous class holds the reference of MyClass ?
Question 2 : What are the fingerprints made over the memory in both cases ?
Question 3 : Which will be best case to use, to increase the performance or to hold the less memory ?  

A:

The inner class instance can access to the container instance memory. In case of name conflict, you can force the usage of the container instance through its class name, thanks to MyClass.this.
Answer 1: yes
Answer 2: 2 class are created ~ same fingerprint. In the first case, the listener class can be reuse elsewhere.
Answer 3: an inner class can access to container instances' properties and methods → very useful, but when a listener class can be shared between multiple usage, it's use less class. The best way is the way that factorize more codes.

