views:

1073

answers:

10

I know anonymous classes save typing, if 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 these language-feature? Does it make sense and you use it regularly? Makes the saved typing the code clearer, more understandable and more maintainable? Or does anonymous classes make the code less readable?

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

+1  A: 

It makes sense to use them, but you must be aware of whats being done underneath. I only use them if I need a class to do something very specific that I don't need anywhere else.

Megacan
+3  A: 

We use anonymous classes regurlarly. I find them easy to use for implementing interfaces that have only one or two methods and that where the functionality isn't used anywhere else. If you use the same functionality again somewhere else there should be a real class to be reused.

boutta
Or the anonymous inner class and some of the code surrounding it moved into a separate method. Amongst other things it makes your "API" less cluttered.
Tom Hawtin - tackline
+3  A: 

I use anonymous classes mostly for interfaces that have only a single method, i.e. Runnable or ActionListener. Most larger interfaces warrent their own classes or implementation in an already existing class. And as it is my opinion I don’t need arguments to support it.

Bombe
+1  A: 

It depends what you compare them to. I'd rather have them than not have them, but then I'd rather be able to supply plain code blocks to methods like Arrays.sort() than having to explicitly create a class containing my implementation of compare().

Stephen Denne
+10  A: 

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.

coobird
A: 

There is nothing inherantly different or special about anon classes. They are ultimately just syntactical sugar with support for referencing the outer class. This makes it easier to author adapters - just like most of the Iterator implementations returned by the Collections framework.

mP
Referencing the outer class is also possible with normal inner classes. Anonymous classes get no classname, that's the most important difference. They can be used only once this way.
Mnementh
A: 

I use Anonymous classes mostly a) shorthand notation if the interface has one or two methods and it wont affect the readability

b) situation where I wont be able to justify creation of a new class, for example In swing when you have to attach an actionlistner to lets a JButton for some trivial operation.

+6  A: 

My opinion is Anonymous class makes the code less readable.For implementing listners anonymous class is useful.For developing GWT application anonymous class is better choice. For this cases if we are not using anonymous class then the number of lines in code will be increased.

BlackPanther
+1. I don't like the way the split up the logical flow of the class and it makes them harder to unit test
MrWiggles
A: 

If limiting scope and access as much as possible is a good thing, then anonymous classes are very good. They are limited in scope to the one class that needs them. When that's appropriate, I'd say anonymous classes are good.

The instant you duplicate the same function, it becomes a bad idea. Refactor it into a public class that stands on its own. IDEs with refactoring features make that easy.

duffymo
But private inner classes are also restricted in scope to their outer class. What makes anonymous classes better?
Mnementh
A: 

I agree with what many others have said in that they are useful for small interfaces when only used once. But I would also add the restriction that if code external to the anonymous class has to be altered for it to work, then don't use an anonymous class.

If you have to start declaring variables as final to accommodate the anon class since it references them, then use an inner class instead. I have also seen some bad code smells where final arrays (of size 1) are used to return results from anon classes.

Robin