Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use anonymous classes? [closed]

I run into some code which contains anonymous classes. I haven't met with anonymous classes before, so I did some research on them.

My main area of interest is java, so I checked Oracle's tutorial of anonymous classes. I understand the mechanism and I see the point of the examples but in my opinion using anonymous classes makes the code hard to read and can cause a lot of headache.

Are there any cases when it is unavoidable to use anonymous classes or it is advised to use them instead of named classes?

like image 778
Tamas G. Avatar asked Nov 30 '15 11:11

Tamas G.


2 Answers

Are there any cases when it is unavoidable to use anonymous classes

No, you can always just use a private inner class instead of an Anonymous class.


using anonymous classes makes the code hard to read and can cause a lot of headache

This very much depends on how you use anonymous classes. Consider the following example:

new Thread(new Runnable() {
    @Override
    public void run() {
        // do something
    }
}).start();

In this example you create a Runnable which is run by a thread. If you wouldn't use an anonymous class you'd have to write it as follows:

private class SomeClass implements Runnable {
    @Override
    public void run() {
        // TODO Auto-generated method stub

    }
}

and use it as:

new Thread(new SomeClass()).start();

With the first possibility you can directly see what that thread is doing, in the second possibility you'll first have to find the class that is used here.


Another advantage of anonymous classes. You can do the following:

// define some constant which can be used in the anonymous class:
final String someStr = "whatever";

new Thread(new Runnable() {
    @Override
    public void run() {
        // use the String someStr directly
        System.out.println(someStr);
    }
}).start();

You can use the constant which is declared in the code where the anonymous class is defined. If you'd use a private inner class you'd have to give these constants to the constructor of the class in order to use them!

like image 63
ParkerHalo Avatar answered Oct 23 '22 12:10

ParkerHalo


You can always avoid using of anonymous classes by defining new class implicitly. However in some cases it's better to use anonymous classes. Consider following example:

void main() {
   new Thread(new Runnable(){
      public void run() {
        System.out.println("Hello world");
      }
   }).start();
}

Code snippet above is more readable and shorter then defining new class.

public class MyRunnable implements Runnable {
   public void run() {
     System.out.println("Hello world");
   }
}


void main() {
   new Thread(new MyRunnable()).start();
}

In Java 8 labmda can be used instead of anonymous class in some cases

Runnable task = () -> { System.out.println("hello world"); };
new Thread(task).start();
like image 5
Anton Avatar answered Oct 23 '22 11:10

Anton