Anonymous Inner Class Implementing an Interface Tutorial

An anonymous inner class is a nested class defined without any name. An anonymous class is entirely dependent upon either a superclass or interface in order to work properly. This tutorial will focus on the version that is dependent upon an interface. You may also want to brush up on your Runnable skills by watching my Creating a Thread Part Three Tutorial. Also I highly recommend understanding all the concepts in my Anonymous Inner Class Extending a Superclass Tutorial as well.
public interface Runnable {
     public abstract void run();
}
class Tester {
     public static void main(String args[]){
         Runnable r = new Runnable() {
             public void run() {
                 System.out.println("Running ...");
             }
         };
         new Thread(r).start();
     }
}
In the example above, the anonymous inner class begins with the opening curly brace directly after the new Runnable(), the anonymous inner class ends at the closing curly brace directly before the semicolon above the statement new Thread(r).start();. When new Thread(r).start(); is invoked the output to the console will be Running ....
What purpose does an anonymous inner class like the one above serve? They are a fantastic shortcut when you simply want to create an object that you can override an abstract method on without creating a bunch of cluttered up code. In the example above we simply want to invoke the run() method from the Runnable interface. How would we do that without using an anonymous inner class? First we would need to create a brand new class that would implement Runnable. Second we would need override the run() method and just to be thorough we would include an @Override annotation. Finally we would create an instance of the new subclass and pass it as a parameter to a new Thread object constructor. Then we will need to invoke the start() method of the new Thread instance. So as you can see, the anonymous inner class streamlines our code.

Anonymous inner classes can show up in some unexpected places, I'll demonstrate that in the video tutorial using the code below.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md AnonymousInnerInterface
C:\Java>cd AnonymousInnerInterface
C:\Java\AnonymousInnerInterface>Notepad AnonymousInnerInterface.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


//public interface Runnable {
//    public abstract void run();
//}

class Tester {
    public static void main(String args[]){
        Runnable r = new Runnable() {
            @Override
            public void run() {
                System.out.println("Running ...");
            }
        };
        new Thread(r).start();
    }
}

class Tester2 {
    public static void main(String args[]){
        new Thread(new Runnable() {
            @Override
            public void run() {
                int i=0;
                while(i < Integer.MAX_VALUE) { System.out.println("Strange, but cool syntax ... "+ i++); }
            }
        }).start();
    }
}

Now switch back to the command prompt (CMD) and type in javac AnonymousInnerInterface.java and press Enter.
Now type in java AnonymousInnerInterface and press Enter.


C:\Java\AnonymousInnerInterface>javac AnonymousInnerInterface.java
C:\Java\AnonymousInnerInterface>java AnonymousInnerInterface
Running ...

and 

Strange, but cool syntax ... 0-Integer.MAX_VALUE


Final thoughts

Learning advanced concepts are meant to make your life easier by allowing you to streamline more basic operations. This is another one of those tutorials where if you are new to the concept then you should wait a day to let the concept sink in the watch the video again. I am a believer that frustration and confusion are simply signs that you are learning. Don't give up, keep reviewing until you have your ah-ha moment and then move on to the next thing.


Tutorials