Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

Can callbacks in java be implemented without anonymous classes and lambda expressions?

Most of the research I have done keeps coming up with the same thing which is java implements callbacks using anonymous classes before java 1.8 or lambada expressions in java 1.8 and above.

It has been stated on the “Is a function callback in Java also an anonymous class?” question that callbacks() can be implemented 2 ways in java which makes sense to me. ( correct me if I am wrong )

From code example #1. below, YourMouseListener is defined so all we have to do is instantiate an object and pass that instance to a method, meaning callbacks() can be implemented without anonymous classes or lambda expressions.

In code example #2 solution is implemented with anonymous class which is fine, I just don’t see that it brings any functionality that can’t be done with example #1 methodology.

  1. So, actually, the first way to implement this callback is you declare a class extends MouseAdapter
public class YourMouseListener extends MouseAdapter(){
    public void mouseEntered(MouseEvent e) { ... } 
} 

// then create an instance and passes to it:

c.addMouseListener(new YourMouseListener() );
  1. To minimize code and class declaration, Java enable you to use anonymous classes to achieve callbacks() like:
c.addMouseListener(new MouseAdapter() {
    public void mouseEntered(MouseEvent e) {
        System.out.println("Mouse entry event: " + e
                + " on button: " + buttonLabel);  }});

So my question is, in Java, can callbacks be implemented without anonymous classes and lambda expressions?

Thanks in advance.

question from:https://stackoverflow.com/questions/66067297/can-callbacks-in-java-be-implemented-without-anonymous-classes-and-lambda-expres

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, of course! Callbacks, event listeners, etc. are just types. Java gives many ways to create implementations of interfaces, including anonymous classes and lambda expressions (for functional interfaces).

The option of providing implementations using normal classes is always open. Sometimes it's even preferred, as in cases where the code would be duplicated when the same kind of callback is passed for different events.

The drive for anonymous classes and lambda expressions is brevity and, of course, the fact that callbacks are usually fit-for-purpose code that you use for one event (such as a specific listener code for specific buttons - it's only in rare cases that one needs to reuse code for things like mouse click events).

In many cases, it's convenient to write just the code for it using a lambda expression (when a functional interface applicable) or an anonymous class, instead of writing a separate class for it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...