If each listener is unique, you probably want to use anonymous classes (first example). If you would otherwise have to rewrite the same code over and over, then implementing it in a named class (as in the second example) would be preferable, so you can just reuse the same listener.
However, instead of extending the ActionListener (as in your second example), you will probably find that including a listener implementation in another class (even an inner class that implements ActionListener) provides a better logical separation of your code.
source share