You stumble over it more.
In a sense, there is only one way to create a listener: there must be a class object that implements an ActionListener , which means that the class has an actionPerformed method.
There are three ways to do this:
You can change the class that you already use for something else by marking it as an implementation of ActionListener and adding the actionPerformed method. This saves you the creation of a new class - in most cases, saving negligible cost, but otherwise a completely different code. A few cases where existing
You can create a new named class. This is useful if you think the name will be meaningful to someone. If you really use names like "MyListener", this is a hint that no, nobody cares about the name.
Finally, and usually you can create an unnamed class. If all you want to do is add a code snippet as a listener.
Whatever your choice, it is extremely unlikely to have any noticeable effect on the time or memory performance of your finished system. The choice should be dictated by concerns regarding readability and maintainability.
source share