Double colon (::) operator in Java

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 operatorimportjava.util.stream.*;classGFG {publicstaticvoidmain(String[] args){// Get the streamStream<String> stream= Stream.of("Geeks","For","Geeks","A","Computer","Portal");// Print the streamstream.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 operatorimportjava.util.stream.*;classGFG {publicstaticvoidmain(String[] args){// Get the streamStream<String> stream= Stream.of("Geeks","For","Geeks","A","Computer","Portal");// Print the stream// using double colon operatorstream.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 methodsimportjava.util.*;classGFG {// static function to be calledstaticvoidsomeFunction(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 operatorlist.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 methodsimportjava.util.*;classGFG {// instance function to be calledvoidsomeFunction(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 operatorlist.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 methodsimportjava.util.*;importjava.util.function.*;classTest {// super function to be calledString print(String str){return("Hello "+ str +"\n");}}classGFGextendsTest {// instance method to override super method@OverrideString print(String s){// call the super method// using double colon operatorFunction<String, String>func =super::print;String newValue = func.apply(s);newValue +="Bye "+ s +"\n";System.out.println(newValue);returnnewValue;}// Driver codepublicstaticvoidmain(String[] args){List<String> list =newArrayList<String>();list.add("Geeks");list.add("For");list.add("GEEKS");// call the instance method// using double colon operatorlist.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 typeimportjava.util.*;classTest {String str=null;Test(String s){this.str=s;}// instance function to be calledvoidsomeFunction(){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 operatorlist.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 constructorimportjava.util.*;classGFG {// Class constructorpublicGFG(String s){System.out.println("Hello "+ s);}// Driver codepublicstaticvoidmain(String[] args){List<String> list =newArrayList<String>();list.add("Geeks");list.add("For");list.add("GEEKS");// call the class constructor// using double colon operatorlist.forEach(GFG::new);}}Output:Hello Geeks Hello For Hello GEEKS



