0

I am trying to figure out the difference between the use of lambda and the ReferenceMethod, and I didn't figure out how to refer to non-static methods ?

public class ReferenceMethod {

    public void main(String[] argv) {
        Thread t = new Thread(ReferenceMethod::printMessage); // CE
        Thread t2 = new Thread(() -> printMessage());
        t.start();
    }

    public void printMessage() {
        System.out.println("Hello");
    }
}
gil.fernandes
  • 12,978
  • 5
  • 63
  • 76
Aguid
  • 943
  • 2
  • 10
  • 24

5 Answers5

1

Non-static methods only make sense in the context of an instance of the class that define them. These class don't always have a trivial constructor, so you can't expect the instance to be created on the fly and should provide one yourself.

Once you've done so, it's easy to reference them from the instance :

ReferenceMethod myInstance = new ReferenceMethod();
Thread t3 = new Thread(() -> myInstance.printMessage());
Aaron
  • 24,009
  • 2
  • 33
  • 57
1

To reference a non-static method, you need an instance,

  • if you call it from an static method (like main) you'll have : instance::method
  • if you call it from an nontatic method you'll have : this::method
public void refFromNonStatic() {
    Thread t = new Thread(this::printMessage); //ref non-static method in non-static context
    t.start();
}

public void printMessage() {
    System.out.println("Hello");
}

public static void main(String[] argv) {
    ReferenceMethod r = new ReferenceMethod();
    Thread tStatic = new Thread(r::printMessage);   //ref to non-static in static context
    Thread t2 = new Thread(() -> r.printMessage());
    t2.start();
    tStatic.start();
    r.refFromNonStatic();
}
azro
  • 53,056
  • 7
  • 34
  • 70
0

Try making the method:

public static void printMessage() {
  System.out.println("Hello");
}

Like how you can't call instance methods in the main, you can't have Method reference with instance methods in the main.

EDIT: And also make your main start with:

public static void main(String[] args) {
  ..
}
Yoshua Nahar
  • 1,304
  • 11
  • 28
-1

Change ReferenceMethod::printMessage to this::printMessage.

-1

To understand the double colon operator in Java I would start here. But basically, when you use static that is attaching that method to that particular class, not an instance of the class. When you remove static that is saying that every instance of this class will have this method. So because you don't have static on your method printMessage(), that means that the class ReferenceMethod would not have a method called printMessage(), but instances of type ReferenceMethod would have the method printMessage().

So assuming that the main method is actually supposed to be static, which I thought was a requirement for any Java program to run, then your code really should look like this. I haven't tried compiling this, so correct me if I'm wrong.

public class ReferenceMethod {
    public static void main(String[] argv) {
        Thread t = new Thread(ReferenceMethod::printMessage); // CE
        Thread t2 = new Thread(() -> printMessage());
        t.start();
    }

    public static void printMessage() {
        System.out.println("Hello");
    }

}
arjabbar
  • 6,044
  • 4
  • 30
  • 46