by Dinesh Thakur Category: Inheritance

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);
class AnonymousClass
     public static void main(String[] arga)
      outer outobj = new outer(); //create instance of outer class

Anonymous Inner Classes in Java Example

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles