Method References to Instance Methods
To pass a reference to an instance method on a specific object, use the syntax: objRefName::methodName. This syntax is similar to that used for a static method, except that an object reference is used instead of a class name. The following program demonstrates a static method reference. In the program, notice that myOldMobile( ) and myNewMobile( ) are instance methods of MyMobile. Inside main( ), an instance of MyMobile called ‘my’ is created. This instance is used to create the method reference to myOldMobile( ) and myNewMobile( ) in the call to ‘my’.
Program
interface Mobile { String myMobile(); } class MyMobile { String myOldMobile() { return "Old : Samsung Galaxy S6"; } String myNewMobile() { return "New : Samsung Galaxy S9"; } } class Javaapp { static void showMyMobile(Mobile mo) { System.out.println(mo.myMobile()); } public static void main(String[] args) { MyMobile my = new MyMobile(); showMyMobile(my::myOldMobile); showMyMobile(my::myNewMobile); } }