How is a method or inner class or class?

So, I came across several ways to implement an ActionListener, and I wonder if anyone can get me through the differences in how each works, and are there any reasons or advantages for using one over the other?

The first one is given in the code block:

public void actionPerformed(ActionEvent arg0) { // CODE HERE } 

The second method I saw was in another block of code:

 private class myListener implements ActionListener { // CODE HERE } 

The third is just a separate class for ActionListener, with similar code as above, but inside a separate class.

I am wondering if the method approach is more efficient, since for each object there is no need to create new objects, just refer to this as an ActionListener, and not the new myListener() link. Thanks.

+4
source share
3 answers

There is no speed difference in any of the options; You will always have an object that implements the ActionListener interface. By avoiding an instance of a separate class, you simply save a few bytes of memory.

Your choice really should be based on what makes sense for your code, structurally. For example, if your public class implements an ActionListener, it may look strange to those who use this class, especially if the ActionListener behavior should be closed to the class and not used outside of it.

So this is basically a choice of what you think looks better in your code; the only real difference will be with regard to access to the field / method (for example, a separate, non-inner class will not have access to private methods and fields of your class, an anonymous inner class will not be able to access non-finite variables by the placement method, etc.) .

+6
source

I do not like or do not use "implements ActionListener".

I like and use anonymous inner classes, for example:

  btnPurplescreen.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Color baseColor = Color.BLUE; panelImage.setBackground(baseColor); panelReference.setBackground(baseColor); panelReference2.setBackground(baseColor); baseType = BaseType.PURPLE; } }); 
+2
source

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.

+1
source

Source: https://habr.com/ru/post/1401284/


All Articles