Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Javatutorials Anonymous Inner Classes - Anonymous Inner Classes tutorial

Related Tutorials


 
 

Share on Google+Share on Google+

Anonymous Inner Classes - Anonymous Inner Classes tutorial

Advertisement
Anonymous Inner Classes - Anonymous Inner Classes tutorial

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

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 18, 2011

Related Tutorials

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.
DMCA.com