Java Callback Simple Sample Part2 Anonymous Class Example


public interface myCallback {
    void onSuccess();
    void onError(String err);
}



public class App {

    public static void main(String[] args) {
        //call your method use anonymous class
        doSomething("list your Params", new myCallback(){  
            @Override
            public void onSuccess() {
                // no errors
                System.out.println("Done");
            }

            @Override
            public void onError(String err) {
                // error happen
                System.out.println(err);
            }
        });
    }

    private void doSomething(String param, // some params..
                             myCallback callback) {
        // now call onSuccess whenever you want if results are ready
        if(results_success)
            callback.onSuccess();
        else
            callback.onError(someError);
    }

}

Recommended Posts

Java Callback Simple Sample Part2 Anonymous Class Example
A simple sample callback in Java
Java anonymous class
Kotlin Class part.2 to send to Java developers
Anonymous class (anonymous class)
Creating a matrix class in Java Part 1
CICS-Run Java application-(1) Run a simple sample app
Java class methods
[Java] Class inheritance
java Scanner class
Java HashMap class
[Java] Generics sample
java callback pattern
Java sample code 02
Java sample code 03
Selenium sample (Java)
java (abstract class)
Java GUI sample
java practice part 1
Java sample code 04
[Java] Nested class
About Java class
Java sample code 01
[java] abstract class
[Java] Object class
Java local class
A quick review of Java learned in class part4
Implementation example of simple LISP processing system (Java version)
A quick review of Java learned in class part3
A quick review of Java learned in class part2