The double colon (::) operator, also known as method reference operator in Java, is used to call a method by referring to it with the help of its class directly. They behave exactly as the lambda expressions. The only difference it has from lambda expressions is that this uses direct reference to the method by name instead of providing a delegate to the method.
Syntax:
<Class name>::<method name>
Example: To print all elements of the stream:
- Using Lambda expression:
stream.forEach( s-> System.out.println(s));
Program:
// Java code to print the elements of Stream// without using double colon operatorÂÂimportjava.util.stream.*;ÂÂclassGFG {   Âpublicstaticvoidmain(String[] args)   Â{       Â// Get the stream       ÂStream<String> stream           Â= Stream.of("Geeks","For",                       Â"Geeks","A",                       Â"Computer",                       Â"Portal");       Â// Print the stream       Âstream.forEach(s -> System.out.println(s));   Â}}Output:Geeks For Geeks A Computer Portal
- Using double colon operator:
stream.forEach( System.out::println);
Program: To demonstrate the use of double colon operator
// Java code to print the elements of Stream// using double colon operatorÂÂimportjava.util.stream.*;ÂÂclassGFG {   Âpublicstaticvoidmain(String[] args)   Â{       Â// Get the stream       ÂStream<String> stream           Â= Stream.of("Geeks","For",                       Â"Geeks","A",                       Â"Computer",                       Â"Portal");       Â// Print the stream       Â// using double colon operator       Âstream.forEach(System.out::println);   Â}}Output:Geeks For Geeks A Computer Portal
When and how to use double colon operator?
Method reference or double colon operator can be used to refer:
- a static method,
- an instance method, or
- a constructor.
How to use method reference in Java:
- Static method
Syntax:
(ClassName::methodName)
Example:
SomeClass::someStaticMethod
Program:
// Java code to show use of double colon operator// for static methodsÂÂimportjava.util.*;ÂÂclassGFG {   Â// static function to be called   ÂstaticvoidsomeFunction(String s)   Â{       ÂSystem.out.println(s);   Â}   Âpublicstaticvoidmain(String[] args)   Â{       ÂList<String> list =newArrayList<String>();       Âlist.add("Geeks");       Âlist.add("For");       Âlist.add("GEEKS");       Â// call the static method       Â// using double colon operator       Âlist.forEach(GFG::someFunction);   Â}}Output:Geeks For GEEKS
- Instance method
Syntax:
(objectOfClass::methodName)
Example:
System.out::println
Program:
// Java code to show use of double colon operator// for instance methodsÂÂimportjava.util.*;ÂÂclassGFG {   Â// instance function to be called   ÂvoidsomeFunction(String s)   Â{       ÂSystem.out.println(s);   Â}   Âpublicstaticvoidmain(String[] args)   Â{       ÂList<String> list =newArrayList<String>();       Âlist.add("Geeks");       Âlist.add("For");       Âlist.add("GEEKS");       Â// call the instance method       Â// using double colon operator       Âlist.forEach((newGFG())::someFunction);   Â}}Output:Geeks For GEEKS
- Super method
Syntax:
(super::methodName)
Example:
super::someSuperClassMethod
Program:
// Java code to show use of double colon operator// for super methodsÂÂimportjava.util.*;importjava.util.function.*;ÂÂclassTest {   Â// super function to be called   ÂString print(String str)   Â{       Âreturn("Hello "+ str +"\n");   Â}}ÂÂclassGFGextendsTest {   Â// instance method to override super method   Â@Override   ÂString print(String s)   Â{       Â// call the super method       Â// using double colon operator       ÂFunction<String, String>           Âfunc =super::print;       ÂString newValue = func.apply(s);       ÂnewValue +="Bye "+ s +"\n";       ÂSystem.out.println(newValue);       ÂreturnnewValue;   Â}   Â// Driver code   Âpublicstaticvoidmain(String[] args)   Â{       ÂList<String> list =newArrayList<String>();       Âlist.add("Geeks");       Âlist.add("For");       Âlist.add("GEEKS");       Â// call the instance method       Â// using double colon operator       Âlist.forEach(newGFG()::print);   Â}}Output:Hello Geeks Bye Geeks Hello For Bye For Hello GEEKS Bye GEEKS
- Instance method of an arbitrary object of a particular type
Syntax:
(ClassName::methodName)
Example:
SomeClass::someInstanceMethod
Program:
// Java code to show use of double colon operatorÂ// for instance method of arbitrary typeÂÂÂimportjava.util.*;ÂÂÂclassTest {ÂÂ Â Â ÂString str=null;Â Â Â Â Â ÂTest(String s)Â Â Â Â{Â Â Â Â Â Â Â Âthis.str=s;Â Â Â Â}Â Â Â Â// instance function to be calledÂÂ Â Â ÂvoidsomeFunction()ÂÂ Â Â Â{ÂÂ Â Â Â Â Â Â ÂSystem.out.println(this.str);ÂÂ Â Â Â}Â}ÂÂÂclassGFG {ÂÂÂÂ Â Â Âpublicstaticvoidmain(String[] args)ÂÂ Â Â Â{ÂÂÂÂ Â Â Â Â Â Â ÂList<Test> list =newArrayList<Test>();ÂÂ Â Â Â Â Â Â Âlist.add(newTest("Geeks"));ÂÂ Â Â Â Â Â Â Âlist.add(newTest("For"));ÂÂ Â Â Â Â Â Â Âlist.add(newTest("GEEKS"));ÂÂÂÂ Â Â Â Â Â Â Â// call the instance methodÂÂ Â Â Â Â Â Â Â// using double colon operatorÂÂ Â Â Â Â Â Â Âlist.forEach(Test::someFunction);ÂÂ Â Â Â}Â}Output:Geeks For GEEKS
- Class Constructor
Syntax:
(ClassName::new)
Example:
ArrayList::new
Program:
// Java code to show use of double colon operator// for class constructorÂÂimportjava.util.*;ÂÂclassGFG {   Â// Class constructor   ÂpublicGFG(String s)   Â{       ÂSystem.out.println("Hello "+ s);   Â}   Â// Driver code   Âpublicstaticvoidmain(String[] args)   Â{       ÂList<String> list =newArrayList<String>();       Âlist.add("Geeks");       Âlist.add("For");       Âlist.add("GEEKS");       Â// call the class constructor       Â// using double colon operator       Âlist.forEach(GFG::new);   Â}}Output:Hello Geeks Hello For Hello GEEKS
