Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java/Android: anonymous local classes vs named classes

I would like to ask what is the good practice on using anonymous classes vs. named inner classes?

I am writing an Android application, which includes many UI elements (buttons, text fields, etc). For many of them I need some kind of listeners, so in onCreate of the application I have bunch of quite small anonymous classes like:

someButton.setOnClickListener(
    new View.OnClickListener() {
        public void onClick(View v) {
            // do something...
        }
    }
);

Each of of such anonymous class is 5 - 20 lines big - small enough and fits good for recommendations from Java™ in a Nutshell book:

In general, you should consider using an anonymous class instead of a local class if:

  • The class has a very short body.
  • Only one instance of the class is needed.
  • The class is used right after it is defined.
  • The name of the class does not make your code any easier to understand.

But the problem, IMO, is that onCreate becomes quite big and the code becomes more complex to read and understand by quick looking in to it. It is still easy to understand, but simply too big.

So what would be better practice in such case - have bunch of small inner sub-classes, where each of it is nicely separated , but used just once or better keep using anonymous classes instead?

like image 835
Laimoncijus Avatar asked Aug 26 '10 16:08

Laimoncijus


People also ask

What is the advantage of anonymous class in Java?

Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local class only once.

When Should anonymous inner classes be used?

An anonymous inner class can be useful when making an instance of an object with certain “extras” such as overriding methods of a class or interface, without having to actually subclass a class. Tip: Anonymous inner classes are useful in writing implementation classes for listener interfaces in graphics programming.

What is Anonymous classes in Android?

Android uses anonymous inner classes to great effect. Anonymous inner classes are basically developer shorthand, allowing the developer to create, define, and use a custom object all in one “line.” You may have seen examples of the use of anonymous inner class in sample code and not even realized it.

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.


2 Answers

I don't think there is a clear answer one way or the other. Both styles work fine, its really just what you prefer.

Another option is to have

the contents of each onClick by a single function call, which will keep the anonymous classes very short. I.e:

someButton.setOnClickListener(
    new View.OnClickListener() {
        public void onClick(View v) {
            doSomeButtonClick();
        }
    }
);


private void doSomeButtonClick() {
  // do something
}
like image 79
Cheryl Simon Avatar answered Sep 30 '22 22:09

Cheryl Simon


Refactor the onCreate() into separate methods grouped by common functionality so you have logical units. If the GUI is complex then it will help you later.

EDIT:

Also, since you are by default indented more by a code formatter when inside an anonymous class your lines need to be shorter to avoid being broken over several lines by the formatter making it longer. This is usually the thing that make it clear that it would be a good time to extract the class and give it a name.

like image 24
Thorbjørn Ravn Andersen Avatar answered Sep 30 '22 21:09

Thorbjørn Ravn Andersen