Java Stream How to - Reference static and object method








Question

We would like to know how to reference static and object method.

Answer

//  ww  w .  j a  va 2 s. c o m
public class Main {

    public static void main(String[] args) {
        // MyClass class' static access
        Runnable r1 = Main::staticWork;

        // MyClass object's access
        Main myClass = new Main();
        Runnable r2 = myClass::work;
    }

    // Same method signature with Runnable's run method => void ***()
    public static void staticWork() {
        System.out.println("-- Static method body --");
    }

    // Same method signature with Runnable's run method => void ***()
    public void work() {
        System.out.println("-- Non-Static method body --");
    }


}