Minecraft PC IP: play.cubecraft.net

NanoNet

Dedicated Member
May 23, 2014
1,044
357
158
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
 

repository

Well-Known Member
May 3, 2015
37
129
108
25
www.google.com
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
View attachment 44073
View attachment 44077
And then to use the actionPerformed method?

OR, is it more effective to approach the event via lambda?
View attachment 44076
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

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
 
  • Like
Reactions: NanoNet

NanoNet

Dedicated Member
May 23, 2014
1,044
357
158
IKEA
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
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
 

alyphen

Well-Known Member
Jan 4, 2014
101
109
118
27
127.0.0.1
seventh-root.com
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.
 
  • Like
Reactions: NanoNet

NanoNet

Dedicated Member
May 23, 2014
1,044
357
158
IKEA
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.
I didn't even know you could do that, thanks!
 

not2excel

Well-Known Member
Jun 24, 2015
35
28
98
30
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)
 
  • Like
Reactions: NanoNet
Members Online

Team online

Latest profile posts

ToonBer wrote on Frontlane's profile.
1713270171790.png
ToonBer wrote on Capitan's profile.
1713269798868.png
ToonBer wrote on JokeKaedee's profile.
hello there 🍰
GiGaGekkies wrote on ToffeeET's profile.
Happy Birthday xx
Reesle wrote on ToffeeET's profile.
Happy Birthday! 🎈
Top Bottom