What are the advantages of an anonymous inner class (over a non-anonymous inner class)?

Consider this (anonymous):

speakBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { mTts.speak(words.getText().toString(), TextToSpeech.QUEUE_ADD, null); }}); 

against. this: (not anonymous):

 class MyOuterClass { private class MyOnClickListener implements OnClickListener { @Override public void onClick(View view) { mTts.speak(words.getText().toString(), TextToSpeech.QUEUE_ADD, null); } } // later (inside some method)... speakBtn.setOnClickListener(new MyOnClickListener()); } 

With the exception of fewer lines, is there another advantage to the anonymous form?

Is there a performance advantage?

+6
java anonymous-types inner-classes
source share
6 answers

An anonymous inner class takes precedence over the inner class (as in the sample code example) in that it closes over the local variables of the method (although only final locals can be used).

Typically, an inner class can easily be converted to a method with an anonymous inner class, which helps reduce verbosity. If you have an inner class that is so large that you want to make it nonlocal, you might need to think about dealing with your IDE in order to put it in a new file as an outer class.

(These are also local classes, which are normal named inner classes defined inside a method and that are closed by locales.)

+3
source share

The advantage of not being anonymous is that you can reuse the class. I believe that the only reason for using an anonymous inner class is brevity.

+2
source share

Things I like about these inner classes:

  • They have a name. It’s easier for me to debug when I see MyOuterClass$MyOnClickListener in a stack trace instead of MyOuterClass$1 , which happens with an anonymous inner class.
  • Sometimes it helps readability to separate the actual code for the inner class from where you use it. I especially like this if I'm already in a long method or indented more than a level or two.

To comment on your point of view of performance, anonymous inner classes are compiled into regular classes, so there should be no difference in performance.

+2
source share

The advantage is the time you save as a developer, so as not to introduce additional keystrokes. :)

Oh, and that also prevents you from uselessly coming up with a new name for everything (which can cause name conflicts in some cases).

+1
source share

There is one drawback:

If you need more than one inner class, you really need to use an explicitly defined class. For what you described, you do not need it. But later you can decide that you need another object that performs the same functions, but is a different object.

+1
source share

The goal of anonymous classes is to make your required class local. Anonymous classes are used when we are very sure that a particular class A is the only consumer for class B, and not this class B anywhere else. Then we better define that class B is an anonymous class inside Named class A. We write the required logic within the same class, so avoid creating an object from the outside. It will be easy to maintain in terms of maintainability of the code. Anonymous classes make code more concise. They allow you to declare and instantiate a class at the same time. They are similar to local classes, except that they do not have a name. Use them if you need to use a local class only once.

0
source share

All Articles