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");
}
}