0

I am refactoring a class with a public facing interface and thinking about the usage led me to ask:

What is the difference between declaring the following within some larger class (as an instance variable):

private final OnClickListener mButtonOnClickListener = new OnClickListener() {
    @Override
    public void onClick(View view) {            
        //some codes
    }
};

vs declaring as an anonymous inner class as follows (on the fly):

private void someFunctionInClass() {
    someOtherFunctionThatTakesAnOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {            
            //some codes
        }
    });
}

More specifically, is the former still considered an anonymous inner class? I read in this answer that an anonymous inner class

is one that is created AND defined within the body of another class' method

The first example I gave is created and defined within the body of another class but not within another class' method as the second one is. Is it still an anonymous inner class? Furthermore, what is the accepted practice for one vs. another? Is it more efficient to declare (what I think is still) an anonymous inner class as an instance variable because new objects don't need to be recreated?

Community
  • 1
  • 1
Daniel Smith
  • 8,561
  • 3
  • 35
  • 58
  • An anonymous class is an anonymous class. There is still only *one* [anonymous] type created in both cases - the usage of the resulting instance(s) is irrelevant to the type itself. –  Mar 21 '13 at 20:12

5 Answers5

2

Both of these are anonymous classes. Other than scope there is not much difference. But below is a link that can be used in deciding which to use from Local class: Anonymous class: Nested class: Lambda expression: http://docs.oracle.com/javase/tutorial/java/javaOO/whentouse.html

I hope it helps.

Jabir
  • 2,776
  • 1
  • 22
  • 31
1

Those are both anonymous classes. In the first one, you can reuse it, but both are just objects that are created. An anonymous class is necessarily an inner class, and can access any fields of the enclosing class.

I think you may be getting anonymous classes confused with inner classes and static nested classes, which have distinct differences.

Andrew Mao
  • 35,740
  • 23
  • 143
  • 224
0

Both are anonymous classes. Taking the example of a listener, if you intend to use the same listener for two components you can have an instance variable for that else you can directly attach it to the component. So it depends on the requirement.

But mostly its a sort-of one-time use, that is, you avoid creating instance for that. If you intend to reuse it, its better you create a separate class for that.

Sudhanshu Umalkar
  • 4,174
  • 1
  • 23
  • 33
0

Both are anonymous classes. An anonymous class is a constructor (new ClassName()) followed by a class body ({...}).

Javier
  • 12,100
  • 5
  • 46
  • 57
0

In your examples, in both cases you have created anonymous inner class. That it "new OnClickListener() {" I think it does not have overhead since its getting resolved at compile time. People use it all the time.

user1697575
  • 2,830
  • 1
  • 24
  • 37