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.
Java Anonymous Class
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.
1 2 3 4 5 6 |
package com.journaldev.java.examples; public interface Hello { public void sayHello(); } |
Java Anonymous Class Example
Hello
is an interface, let’s see how we can create an anonymous class implementation of Hello interface.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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.
1 2 3 4 5 6 |
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.
1 2 3 4 5 6 |
package com.journaldev.java.examples; public class Hello { public void sayHello(){}; } |
Notice that anonymous classes are expressions and ends with semicolon.
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 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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(){}; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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
- We can use any constructor while creating anonymous class. Notice the constructor argument is being used too.
- 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.
- 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. - You cannot declare static initializers or member interfaces in an anonymous class.
- An anonymous class can have static members provided that they are constant variables.
That’s all for anonymous class in java.