Java Anonymous Class

Filed Under: Java

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
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

Hello is an interface, let’s see how we can create an anonymous class implementation of Hello interface.


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(){};
}

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.


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.

That’s all for anonymous class in java.

Comments

  1. Teekam singh says:

    Nice sir !! i read this article from 2-3 website but still unable to get but here i got some idea Nice work Go ahead . i m in your support.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages