1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Lambda or Implement ActionListener?(Java)

Discussion in 'Software/Coding' started by NanoNet, Jun 12, 2015.

  1. NanoNet

    NanoNet Member

    Joined:
    May 23, 2014
    Messages:
    1,044
    Likes Received:
    345
    Trophy Points:
    83
    Gender:
    Male
    Location:
    IKEA
    My question is: When handling events, (Such as when a button is clicked) Is it more effective to handle the event by implementing ActionListener into your class
    upload_2015-6-12_10-51-4.png
    upload_2015-6-12_11-0-1.png
    And then to use the actionPerformed method?

    OR, is it more effective to approach the event via lambda?
    upload_2015-6-12_10-59-24.png
    and then create a b1Click( ) Method, that handles the event?

    (I know you can also create an Inner class that implements ActionListener, but I don't know if that's any better?)

    Thanks for taking your time to read/reply! :p
     
  2. ChillDylanC

    ChillDylanC Member

    Joined:
    Mar 15, 2015
    Messages:
    593
    Likes Received:
    177
    Trophy Points:
    43
    Gender:
    Male
  3. repository

    repository Member

    Joined:
    May 3, 2015
    Messages:
    37
    Likes Received:
    127
    Trophy Points:
    33
    Gender:
    Male
    Home Page:
    Whatever approach you are going to use there will be a very minimal difference in speed, just do whatever one you're comfortable with doing :). Inner classes can be very useful as they provide a safe template which can only be instantiated within the outer class. You could also use an anonymous class. BUT it's all personal preference and the speed difference will be very miniscule (if you are that worried use the System class to get the current time in nanoseconds and record the difference in time it takes using each method/class). By the way you should @Override the action performed method, since it is inherited from an existing class.

    Hope this helps! :p
     
    NanoNet likes this.
  4. NanoNet

    NanoNet Member

    Joined:
    May 23, 2014
    Messages:
    1,044
    Likes Received:
    345
    Trophy Points:
    83
    Gender:
    Male
    Location:
    IKEA
    Thank you so much! Google searches don't exactly go well for me... xD That's why I love asking on here.

    Lock Please :p
     
  5. alyphen

    alyphen Member

    Joined:
    Jan 4, 2014
    Messages:
    101
    Likes Received:
    96
    Trophy Points:
    28
    Gender:
    Male
    Occupation:
    Technical Consultant
    Location:
    127.0.0.1
    Home Page:
    Just another note, you can do something like the following:

    b1.addActionListener(event -> {
    // Full method body - no ex tra methods required
    });

    I like this approach personally, it looks a little cleaner and cuts down on code.
     
    NanoNet likes this.
  6. NanoNet

    NanoNet Member

    Joined:
    May 23, 2014
    Messages:
    1,044
    Likes Received:
    345
    Trophy Points:
    83
    Gender:
    Male
    Location:
    IKEA
    I didn't even know you could do that, thanks!
     
  7. not2excel

    not2excel Member

    Joined:
    Jun 24, 2015
    Messages:
    35
    Likes Received:
    27
    Trophy Points:
    18
    Now I know that this thread's been deadish for ~10 days now, but I can't help but add in some more information.

    Lambda's in general (note the keyword general) are faster than setting up either an anonymous class or a regular class for what java calls FunctionalInterfaces (eg. just single method interfaces that can replace the old object creation that java used to do with a functional lambda expression)

    Now another important thing to note is that lambda's not only generally make code cleaner, they also have a smaller bytecode footprint. (if you do mess with bytecode frequently, you'd understand what I mean by this)

    @NanoNet not only can you do what @alyphen said with lambdas, you should also do some research on method references, since they make lambda based calls even more cleaner (when used correctly ofc)
     
    NanoNet likes this.