Java Stream How to - Reference instance method and static method








The following code shows how to reference instance method and static method.

Example

/*from w ww . ja v a  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 --");
  }

}