Home ProgrammingJava Anonymous Inner Class

Anonymous Inner Class

by Log Raj Bhatt
0 comment 838 views

Anonymous Inner class:

We have already discussed about two types of inner classes these are: Member nested class and Local inner classes. In this post, we are discussing about third type of inner class i.e. Anonymous Inner Class.

Sometimes we can declare inner class with out name, such types of inner classes are Anonymous Inner Classes. Anonymous here means with no name.

Main purpose of Anonymous Inner Class is just for instance use or one time use. Generally Anonymous Inner Classes are used in GUI based application to implement event handling. We can use anonymous inner class to create threads also because in most of cases there is only one instance of these Thread classes.

We can define anonymous classes in three ways. These may be a bit different in terms of their declaration and behaviors. Let’s see example for each way and explain them separately.

Anonymous inner class that extends a class


File name: AnonymousMain.java

package inner.anonymous;
public class AnonymousMain {
	public static void main(String[] args) {		
		Thread t=new Thread() {
			public void run() {
				for(int i=0;i<10;i++) {
					System.out.println("threadclass "+i);
		for(int i=0;i<10;i++) {
			System.out.println("mainthread "+i);


In the above code we are creating an Anonymous class extending Thread class. The code Thread t=new Thread(){}; perform following three tasks;

      • Define an anonymous class which extends the Thread class.
      • Create an object of that anonymous class.
      • Assign that object to Thread reference t.

name of Class file generated for the above anonymous class is AnonymousMain$1.java. See the Tips at the bottom.


output 1

output 1

Order of output is different in each run. One of the possible output is as follows.

mainthread 0
mainthread 1
threadclass 0
threadclass 1
threadclass 2
threadclass 3
threadclass 4
threadclass 5
threadclass 6
threadclass 7
threadclass 8
threadclass 9
mainthread 2
mainthread 3
mainthread 4
mainthread 5
mainthread 6
mainthread 7
mainthread 8
mainthread 9

Anonymous Inner class that implements an Interface

In the same way we can define a anonymous inner class implementing a interface.


package inner.anonymousInterface;

public class ImplementMain {

	public static void main(String[] args) {		
		Runnable r=new Runnable() {
			public void run() {
				for(int i=0;i<10;i++) {
					System.out.println("Runnableclass "+i);
		Thread t=new Thread(r);
		for(int i=0;i<10;i++) {
			System.out.println("mainThread "+i);


Similar to first example this code also creates an object of anonymous class that implements Runnable interface and assign that to Runnable reference r.


Order of output is different in each run. One of the possible output is as follows.

Anonymous Inner Class 3

mainThread 0
Runnableclass 0
Runnableclass 1
Runnableclass 2
Runnableclass 3
Runnableclass 4
Runnableclass 5
Runnableclass 6
Runnableclass 7
Runnableclass 8
Runnableclass 9
mainThread 1
mainThread 2
mainThread 3
mainThread 4
mainThread 5
mainThread 6
mainThread 7
mainThread 8
mainThread 9

Anonymous inner class inside argument

We also can define an Annonymous Inner Class


package inner.anonymousArgs;
public class InsideArgMain {
	public static void main(String[] args) {
		Thread t=new Thread(new Runnable() {
			public void run() {
				for(int i=0;i<10;i++) {
		for(int i=0;i<10;i++) {


Here also we define anonymous class in the same way but we define it  as the argument for particular function. You may see this kind of example while adding EventListener to gui components.


Order of output is different in each run. One of the possible output is as follows.

Anonymous Inner Class 4


Some tips about Anonymous inner classes

  • Annonymous class can extend only one class at a time.
  • It can implement only one interface at a time i.e. it cannot implement more than one interface at a time.
  • Anonymous class either implement an interface or extends a class but cannot implement and extend at the same time.
  • Separate .class files are generated for each anonymous class.
  • Name of class file is like OuterClass$1.java, OuterClass$2.java and so on.
  • We cannot define constructor inside an Anonymous Inner Class because there is no any name for Anonymous class at programmatic level.

If you have any confusion or if you find any mistake please mail us or comment down us. Happy java learning…

0 comment

Leave a Reply

Do NOT follow this link or you will be banned from the site!