Which is faster? Implementing the listener or create a class field?

Hey guys, just a quick question.

Let’s use the ActionListener, for example. What is the best approach? Implement the ActionListener on my class, or declaring a variable to use it?

Thanks,
Ev1lbl0w

I don’t understand what you mean… either way, we’d have to see code examples to know what you really meant.

Well, it’s basically this: (i’m just using ActionListener as an example)

What is the fastest solution?

  • Implement the ActionListener in my class (in this case, it’s an appstate)

      public class GameAppState extends AbstractAppState implements ActionListener {
      ...
      }
    
  • Create a class field for the listener

      ActionListener actionListener = new ActionListener() {
      ...
      };

Presumably you only create the “actionListener” once… so these are virtually identical.

Edit: style-wise, the first approach is bad because it exposed your listener method publicly to anything accessing the state. Not horrible but many of us find that stinky.

1 Like

Ok, thanks!

@pspeed, Just a little off-topic, I really admire your skill of understanding what is not explicit…I would never guessed what op wanted.

Thanks. I’d like to say it was something I was born with but it comes from reading millions of lines of other peoples’ code. :slight_smile: While useful in the long run, remember that there are some things you just can’t unsee. :slight_smile: (Even before being in the Open Source world, I was a contractor that went in and fixed old C and C++ codebases… for years. The C++ horror I witnessed I do not wish upon anyone.)

1 Like

@pspeed is actually a experimental Google bot

2 Likes

Oh, cmon, C++11 is not that bad :slight_smile: but I definitely see what you mention. Btw Rational Rose does help sometimes you reverse a completely unknown mess - at least you can see dependencies on one screen at once.

Reverse engineering software didn’t exist back then… at least not like it does now. Even today’s software is not that great because for C++ only the compiler can truly understand what the code is doing.

I can understand Java and C code just by reading it, though. C++ requires lots of tools. (Back then we wrote our own.)