Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-14-2013, 11:39 AM   PM User | #1
Zaid
New Coder

 
Join Date: Sep 2012
Location: Mumbai
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Zaid is an unknown quantity at this point
Question Anonymous Code

I recently encountered a code in my java program that is as follows..
Code:
button.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e)
   {
      //Some action here
   }
});
I don't know what is after new ActionListener() but it seems a method of ActionListener is overridden and in some block....huh

Last edited by Zaid; 01-14-2013 at 11:41 AM..
Zaid is offline   Reply With Quote
Old 01-14-2013, 02:45 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Yeah, this is an anonymous inner class. ActionListener itself is an interface, but since it is being invoked and the entire method set is being declared as a part of the class body, this is valid code. Prior to Java 8, it's also the closest thing we have to a closure in Java.
So effectively you create an unnamed instance and provide that as the argument. That particular instance can never be used again.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:26 AM.


Advertisement
Log in to turn off these ads.