Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android and Java: use of runnable

I have read that in Java interfaces can't be instantiated (in the documentation, Interfaces). Runnable, by definition is an interface which should be implemented by some class. But in the following piece of code from one of my Android applications I see that an empty constructor - Runnable()...(I am not sure if it is a constructor) has been used, and an instance has been created - getStatus.

final Runnable getStatus = new Runnable()
{
    public void run()
    {
        TextView uat = (TextView) findViewById(R.id.useAndThrow);
        uat.setText(MyThread.getStatus());
        return;
    }
};

What I have come to know:

  1. This is perhaps an example of anonymous class.
  2. The instance is not actually of interface.

But I am not able to connect the dots. Assuming that the above code was inside the myActivity class, what is being instantiated and how is this anonymous class?

A bit of detail would be great.

like image 935
CuriousSid Avatar asked Jun 15 '12 14:06

CuriousSid


People also ask

What is the use of runnable in Java?

The runnable interface is used to write applications which can run in a separate thread. Any class that implements the runnable interface is called a thread. The code of the thread is executed by the interpreter after the thread is started.

What is Handler and runnable in Android?

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue . Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler it is bound to a Looper .

What is the difference between thread and runnable in Android?

Runnable is an interface which represents a task that could be executed by either a Thread or Executor or some similar means. On the other hand, Thread is a class which creates a new thread. Implementing the Runnable interface doesn't create a new thread.

What is an advantage of using the runnable interface?

When we extend Thread class, we can't extend any other class even we require and When we implement Runnable, we can save a space for our class to extend any other class in future or now.


2 Answers

It's called an "anonymous class". Some notes about it:

  1. You didn't provide it with a name, so one is created for you. In order to see what its real name is, simply write it to the console or read the bytecode.

  2. The anonymous class, if created in the scope of the outer class, has a reference to its fields, just like any inner class. In general, it has the same scope of where it was created.

  3. It's a very common thing to do if all you need is a simple implementation. The more code there is, the more you should consider putting it in a another file (for order, not because you can't).

  4. Runnable is not the only interface that is common. On Android, you use OnClickListener, OnTouchListener, ...

  5. Since the anonymous class has the same scope as to where it was declared, it means that it has a reference. Try to never keep an anonymous class for too long (for example, in a static reference) , since this could lead to memory leaks. On Android it's very crucial, as this sample shows (talks about drawables, but an anonymous class can also have a reference to a context) .

  6. Some of the disadvantages of anonymous classes are that they don't have a constructor, and that it cannot implement more than one interface. It has a very narrow usage, yet it's very popular as it's very easy to use (and read).

like image 183
android developer Avatar answered Oct 04 '22 05:10

android developer


Google for more information on this using "Anonymous innerclass". This "Inner classes in Java, the mystery within" blog post explains quite well the different types of inner classes. See also "3.12. Anonymous Classes".

like image 24
barrel Avatar answered Oct 04 '22 04:10

barrel