Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is usage of anonymous classes in Java considered bad style or good? [closed]

I know anonymous classes save typing when it comes to implementing Listener and similar stuff. They try to be a replacement for some usages of closures.

But what does the community think about the value of this language-feature? Does it make sense and do you use it regularly? Does it make the code clearer, more understandable and more maintainable? Or do anonymous classes make the code less readable?

What is your opinion, and please have examples/arguments handy to support your opinion?

like image 931
Mnementh Avatar asked Jan 28 '09 10:01

Mnementh


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.

In what cases will Anonymous classes be useful?

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 true about anonymous class?

It can extend exactly one class and can implement multiple interfaces. It can extend exactly one class and implement exactly one interface. It can implement multiple interfaces regardless of whether it also extends a class.

When using an anonymous class what is its scope?

Anonymous classes capture local variables that are in the scope of the block in which we have declared the class: int count = 1; Runnable action = new Runnable() { @Override public void run() { System.


1 Answers

I tend to use anonymous inner classes in situations where I don't need to have a full-blown class just to perform some task. For example, if I want to implement an ActionListener or Runnable, but I don't think having an inner class would be necessary. For example, for starting a simple Thread, using an anonymous inner class might be more readable:

public void someMethod() {     new Thread(new Runnable() {         public void run()         {             // do stuff         }     }).start(); } 

In certain cases, such as the example above, it can increase readability, especially for one-time tasks, as the code that is to be executed is all written in one spot. Using an inner class would "delocalize" the code:

public void someMethod() {     new Thread(new MyRunnable()).start(); }  // ... several methods down ... //  class MyRunnable implements Runnable {     public void run()     {         // do stuff     } } 

That said, however, if there is going to be cases where the same thing is going to be repeated, it should indeed be a separate class, be it a regular class or an inner class.

I tend to use anonymous inner classes in programs where I am just trying things out rather than have it as a central feature of an actual application.

like image 73
coobird Avatar answered Sep 18 '22 17:09

coobird