Anonymous Inner Classes - Anonymous Inner Classes tutorial

Anonymous Inner Classes - Anonymous Inner Classes tutorial

Ads

Tutorials   
Java final method Assert in Java Final method in java Autoboxing in Java assert In Java JDBC batch insert using Java bean class Java Singleton Pattern Java Collection API - Java Tutorials Stack Overflow - Java Tutorials End of Year Puzzle The link to the outer class,java tutorial,java tutorials Orientating Components Right to Left,java newsletter,java,tutorial Non-virtual Methods in Java - java tutorials Multicasting in Java - java tutorials,tutorial Creation Time Comparison of Multi Dimensional Array- Java Tutorials Making Exceptions Unchecked - java tutorial,java tutorials Making Enumerations Iterable - JDK 5 Example Which is Faster - LinkedList or ArrayList? LinkedHashMap - Java Tutorials HashMap and HashCode JDK 1.4 the NullPointerException - Java Tutorials Java HashMap - Java Tutorials Java GC java awt package tutorial Java Multi Dimensions Array - Java Tutorials Java HashSet  - Java Tutorials Java Field Initialisation - Java Tutorials Java Comments - Java Tutorials J2EE Singleton Pattern - Design Pattern Tutorials Java - JDK Tutorials garbage collection in java Final Methods - Java Tutorials Exceptional Constructors - Java Tutorials Double-checked locking,java tutorials,java tutorial Disassembling Java Classes - Java Tutorials Java Deadlocks - Java Deadlocks Tutorials, Deadlocks in Java Commenting out your code - Java Tutorials Class names don't identify a class - Java Tutorials Causing Deadlocks in Swing Code Exception in Java - Java Tutorials Catching Exceptions in GUI Code - Java Tutorials boolean comparisons - tutorial Interthread Communication in Java BASIC Java - Java Tutorials Thread Deadlocks - Java Tutorials Autoboxing unboxing in Java - Java Tutorials Assertion in Java Appending Strings - Java Tutorials Anonymous Inner Classes - Anonymous Inner Classes tutorial Assertion in java
Ads

Anonymous Inner Classes

Except the inner class, there are two types of supplementary  inner classes :

The inner class which is declared inside the body of a method is known as the local inner classes. The class declared inside the body of a method without naming it is known as anonymous inner classes.

Here we are discussing  anonymous inner classes.

The anonymous inner classes is very useful in some situation. For example consider a situation where you need to create the instance of an object without creating subclass of a class and also performing additional tasks such as method overloading.

Consider the following code :

button.addActionListener(new ActionListener() {
public void actionPerfored(ActionEvent e)
{
// do something.
}
});

As you can see in the above code, i don't need to create an extra class that implements ActionListener . I initiated anonymous inner class(here it is new ActionListener()) without creating a separate class. You can also perform method overloading by implementing anonymous inner classes.

Advertisements

Share on Google+Share on Google+

Anonymous Inner Classes - Anonymous Inner Classes tutorial

Posted on: April 18, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Anonymous Inner Classes - Anonymous Inner Classes tutorial  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:10
aman kumar
September 30, 2011
anonymous class

An anonymous class is essentially a local class (inside method body) without a name. Instead of defining a local class and then instantiating it, you can often use an anonymous class to combine these two steps
aman kumar
September 30, 2011
anonymous class

An anonymous class is essentially a local class (inside metod body) without a name. Instead of defining a local class and then instantiating it, you can often use an anonymous class to combine these two steps
Abhijeet Tomar
November 17, 2011
Feedback

First of all , i would like to give thanks to making such a very useful website that is 'RoseIndia' as the name the implies the work and providing many or can say more useful topics including in the website and one thing is different from others tutorial or learning website is that the explanation part in a very easiest way... and the last again Thank You all the members of RoseIndia. Best Wishes.
Govindareddy
January 12, 2012
static and non static nested classes

static in the sence we cant access the members of inner class but we can access by means of object
Ramakant Sahu
March 3, 2012
java

very nice discription
gaurav tyagi
March 22, 2012
anonimuse class

please provide daily life example in case of use
Robert Carnegie
April 4, 2012
Well,

Hi! You've miskeyed "actionPerformed". Technically you are creating a new class, but it's a class that doesn't have a name of its own. When the code executes, one object of that class is created. I assume that when the same code is executed again, another object of the same class is created. Also, you can create an anonymous class, from a superclass or an interface, wherever a "new" object declaration is allowed, and not only in arguments. However, the first main use of anonymous inner classes is in cases like yours, to connect your programmed object to the user interface. But the other main use of anonymous inner classes is that they are in lots of questions in the exam, to make the questions deliberately difficult to understand. So, don't relax yet! Once you pass the exam, you can relax!
rahul
April 19, 2012
java

Anonymous Inner Classes haven't body that class is called Anonymous Inner Classes
Neeraj Kumar
June 3, 2012
comment

best answer...
Ramu
June 13, 2012
Anonymous Inner Classes

Please provide real time scenario of this topic with example.

Ads

 

Ads