Java Anonymous inner class

Java anonymous class are like local class or inner class without a name. We can use java anonymous class to declare and instantiate a class at the same time.

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:

  1. Class (may be abstract or concrete).
  2. Interface

Java anonymous class is a nested or local class. You should use them only when you want to use local class only once. Let’s have a look at an example of anonymous class in java program.

package com.journaldev.java.examples; public interface Hello { public void sayHello(); }
Java Anonymous Class Example
package com.journaldev.java.examples; public class AnonymousExample { //nested anonymous class public static Hello hello = new Hello() { @Override public void sayHello() { System.out.println("Hello nested anonymous class"); } }; public static void main(String[] args) { //anonymous class inside method Hello h = new Hello() { @Override public void sayHello() { System.out.println("Hello anonymous class"); } }; h.sayHello(); AnonymousExample.hello.sayHello(); } }


Above Hello class can be an abstract class also, like below.
package com.journaldev.java.examples; public abstract class Hello { abstract void sayHello(); }
Not only that, Hello can be a normal top level class also, like below.
package com.journaldev.java.examples; public class Hello { public void sayHello(){}; }

Anonymous class is defined by calling class constructor followed by class definition code inside curly braces.

Since anonymous class don’t have a name, we can’t define a constructor inside the class code body.

Java anonymous inner class example using class
abstract class Person { abstract void eat(); } class Test { public static void main(String args[]){ Person p=new Person(){ void eat(){System.out.println("nice Product");} }; p.eat(); } }


Output:
nice Product
Internal working of given code
Person p=new Person(){ void eat(){System.out.println("nice fruits");} };

A class is created but its name is decided by the compiler which extends the Person class and provides the implementation of the eat() method.

An object of Anonymous class is created that is referred by p reference variable of Person type.

Internal class generated by the compiler
import java.io.PrintStream; static class TestAnonymousInner$1 extends Person { TestAnonymousInner$1(){} void eat() { System.out.println("nice fruits"); } }
Java anonymous inner class example using interface
interface Eat { void eat(); } class Test { public static void main(String args[]){ Eat e=new Eat(){ public void eat(){System.out.println("nice Product");} }; e.eat(); } }


Output:
nice Product
Internal working of given code
Eatable p=new Eatable() { void eat(){System.out.println("nice fruits");} };
  1. A class is created but its name is decided by the compiler which implements the Eatable interface and provides the implementation of the eat() method.

  2. An object of Anonymous class is created that is referred by p reference variable of Eatable type.

Internal class generated by the compiler
import java.io.PrintStream; static class TestAnonymousInner1$1 implements Eatable { TestAnonymousInner1$1(){} void eat(){System.out.println("nice fruits");} }
Java Anonymous class example with constructor argument

What if our Hello class don’t have no-args constructor? Can we access class variables in the anonymous class? Do we need to override all the methods of class in anonymous class?

Let’s answer above questions with a simple code example.

package com.journaldev.java.examples; public class Hello { protected String s; public Hello(String str){ this.s = str; } public void sayHello(){ System.out.println(s); }; void foo(){}; }
package com.journaldev.java.examples; public class AnonymousExample { public static void main(String[] args) { //anonymous class inside method Hello h = new Hello("abc") { @Override public void sayHello() { System.out.println("Hello anonymous class "+s); } }; h.sayHello(); } }
Java Anonymous Class Important Points
  1. We can use any constructor while creating anonymous class. Notice the constructor argument is being used too.

  2. Anonymous class extends the top-level class and implements the abstract class or interface. So access modifier rules apply as usual. We are able to access protected variable, if we change it to private then we won’t be able to access it.

  3. Since we are extending the Hello class above, we are not required to override all the methods. However if it would have been an interface or abstract class then we have to provide implementation of all the unimplemented methods.

  4. You cannot declare static initializers or member interfaces in an anonymous class.

  5. An anonymous class can have static members provided that they are constant variables.




Instagram