A class that have no name is known as anonymous inner class in java. It should be used if you have to override method of class or interface. Java Anonymous inner class can be created by two ways:
abstract class Person{ abstract void eat(); } class TestAnonymousInner{ public static void main(String args[]){ Person p=new Person(){ void eat(){System.out.println("nice fruits");} }; p.eat(); } }
Person p=new Person(){ void eat(){System.out.println("nice fruits");} };
import java.io.PrintStream; static class TestAnonymousInner$1 extends Person { TestAnonymousInner$1(){} void eat() { System.out.println("nice fruits"); } }
interface Eatable{ void eat(); } class TestAnnonymousInner1{ public static void main(String args[]){ Eatable e=new Eatable(){ public void eat(){System.out.println("nice fruits");} }; e.eat(); } }
It performs two main tasks behind this code:
Eatable p=new Eatable(){ void eat(){System.out.println("nice fruits");} };
import java.io.PrintStream; static class TestAnonymousInner1$1 implements Eatable { TestAnonymousInner1$1(){} void eat(){System.out.println("nice fruits");} }