Question

I'm currently taking a course in Java and I've run into some confusing code.

Example:

Runnable runnable = new Runnable()
        {
            public void run()
            {
                //doStuff
            }
        };

I don't really get what this code is doing.

How can the run method be associated with an instance of a class?

I googled "Runnable" and found out that it is an interface. Am I implementing the interface by declaring the run method between curly brackets ? Can this be done for any interface in java ?

I could use some links/explanations. Thank you!

Was it helpful?

Solution

It's an anonymous inner class that's implementing the interface Runnable. Yes, you can implement any interface this way, although there are reasons why you would or wouldn't in any given case (lack of reusability being a big one in the "wouldn't" column). More about anonymous classes here, but it's basically a convenient form of this:

// Define it
class Foo implements Runnable
{
    public void run()
    {
        // Do stuff
    }
}

// And then use it
Runnable runnable = new Foo();

...provided Foo is an inner (or "nested") class. More about nested classes here.

OTHER TIPS

yes, you are implementing the interface by declaring the run. Yes it can be done for any interface.

This is typically done when you pass an implementation to a method that expects an argument of an Interface type, and you don't have a declared class that is appropriate. You can just implement the interface on the spot, and that code runs. Pretty neat.

I googled "Runnable" and found out that it is an interface. Am I implementing the interface by declaring the run method between curly brackets ? Can this be done for any interface in java ?

Yes!

This code is instantiating an object which implements Runnable. Because we can't actually construct an interface, any code which attempts to do so must provide implementations for the interface's methods in curly brackets. We don't really get to see what class Java is creating to implement Runnable (these are abstract terms).

If you were to do the following:

Runnable runnable = new Runnable()
        {
            public void run()
            {
                System.out.println("I'm running");
            }
        };
runnable.run();

you would see "I'm running" as your output.

In some situation , this sample code will be useful .... test runna = new test()

class test implements Runnable{
        test(){
            Thread t = new Thread(this);
            t.start();
        }
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while(true){
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.print("asd");
            }
        }

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top