Question

Can someone provide me with a good simple example of a regular nested class and then the same class re-written as an anonymous inner class?

Was it helpful?

Solution

Here are examples:

 public class Outer {
     public static class NestedStatic implements Runnable {

         public void run(){
             doSomething();

         }
     public static void main(String[] args){
         Thread nestThread=new Thread(new NestedStatic());
         nestThread.start();

         // now anonymous:
         Thread anonThread=new Thread(new Runnable(){
             public void run(){
                 doSomethingElse();
             });
         anonThread.start(); 
     }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top