Results 1 to 3 of 3
  1. #1
    nat
    nat is offline Member
    Join Date
    Dec 2013
    Posts
    56
    Rep Power
    0

    Default ActionListener help

    Hi,

    I came across this line in a java code

    ActionListener taskPerformer = new ActionListener()

    which made me confused. If ActionListener is an interface, as I have read, how can it be instantiated? I have read that interfaces can not be instantiated (new ActionListener)???

    How to interpret this for example:

    ActionListener taskPerformer = new ActionListener() //is this ok?
    {
    public void actionPerformed(ActionEvent e)
    {
    try
    {
    ....
    .....
    .....
    }
    }
    }

    Can anybody help me please with an explanation?
    Thanks
    Last edited by nat; 12-14-2013 at 11:09 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: ActionListener help

    The syntax new T( ... ) { ... } is the instantiation of an anonymous class; T is an interface or a superclass of the anonymous class and { ... } is the body of the anonymous class; so you're istantiating a class, not an interface.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    nat
    nat is offline Member
    Join Date
    Dec 2013
    Posts
    56
    Rep Power
    0

Similar Threads

  1. Actionlistener q
    By stuckonjava in forum New To Java
    Replies: 2
    Last Post: 05-16-2012, 08:17 PM
  2. ActionListener Help
    By rakosky in forum AWT / Swing
    Replies: 4
    Last Post: 04-06-2012, 03:59 PM
  3. Please help with actionlistener
    By ADustedEwok in forum New To Java
    Replies: 5
    Last Post: 12-08-2011, 10:04 PM
  4. ActionListener
    By jaylimix in forum Java Applets
    Replies: 5
    Last Post: 11-06-2011, 06:05 PM
  5. Please Help With ActionListener
    By Daman12 in forum New To Java
    Replies: 29
    Last Post: 10-26-2011, 07:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •