Anonymous Inner Class

1.4K views 5 minutes read
A+A-
Reset

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

Example:

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);
				}
			}
		};
		
		t.start();
		for(int i=0;i<10;i++) {
			System.out.println("mainthread "+i);
		}
	}
}

Explanation:

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:

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.

Example:

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);
		t.start();
		for(int i=0;i<10;i++) {
			System.out.println("mainThread "+i);
		}
	}
}

Explanation:

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

Output:

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

Example:

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++) {
					System.out.println("InArgumrnt-"+i);
				}
			}
		});
		t.start();
		for(int i=0;i<10;i++) {
			System.out.println("MainThread-"+i);
		}
	}
}

Explanation:

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.

Output:

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

Anonymous Inner Class 4

MainThread-0
MainThread-1
MainThread-2
MainThread-3
MainThread-4
MainThread-5
MainThread-6
MainThread-7
MainThread-8
InArgumrnt-0
InArgumrnt-1
InArgumrnt-2
InArgumrnt-3
InArgumrnt-4
MainThread-9
InArgumrnt-5
InArgumrnt-6
InArgumrnt-7
InArgumrnt-8
InArgumrnt-9

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…

Leave a Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Index

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.