Is usage of anonymous classes in Java considered bad style or good? -


I know that avoid typing anonymous classes when it comes to applying listeners and similar things, Try to replace the uses.

But what does the community think about the value of this language's specialty? Does this make sense and you use it regularly? Does this make the code clear, more understanding and more maintainable? Or do anonymous classes make the code less readable?

What is your opinion, and please make simple examples / arguments in support of your opinion?

Post text "itemprop =" text ">

I use anonymous internal organs in situations, where I do not need a full-fledged class to do some work. For example, If I want to implement a ActionListener or Runnable , but I do not think it would be necessary to be an internal class, for example, a simple thread < To start / code>, an anonymous internal Using C can be more readable:

  some method of public void () {new thread (new runleball) {run public void () {// do stuff}}). ();}  

In some cases, such as the above example, it can increase readability, especially for one-time operations, the code that is executed is all a Is written in place. An internal class will use "delocalize" code:

  public zeros some method () {New Thread (New MyRunnable ()).} // ... in many ways below ... // Class MyRunnable Tools Runnabel {Public Zero Run () {// do stuff}}  

He said, however, that if there are such cases where the same thing is being repeated, it should actually be a separate class, it is a regular class or internal class.

I use anonymous internal classes in such programs where I am just trying to figure it out as a central feature of a real application.


Comments