by Dinesh Thakur

Anonymous inner classes are the local inner classes that are declared without a name. All of the code for the anonymous class is coded within the method where we need to create an instance of the anonymous class. Since anonymous inner classes do not have a name so you cannot use the new keyword in the usual way to create an instance of the class. In fact, anonymous inner classes are declared and instantiated at the same time. 

class outer

{
      private double i = 11.5 ;
      private static String str = "Hi Java World";
      public void display()
      {
           // Anonymous class
           Object Obj=new Object()
          {
             public String toString()
                                   { return("Anonymous Class");}
           };
        
           System.out.println("i = " + i);
           System.out.println("str = " + str);
           System.out.println(Obj.toString());
        }
}
class AnonymousClass
{
     public static void main(String[] arga)
     {
      outer outobj = new outer(); //create instance of outer class
      outobj.display();
     }
 }

Anonymous Inner Classes in Java Example