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
Â
Âimport
java.util.stream.*;
Â
Âclass
GFG {
   Â
public
static
void
main(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
Â
Âimport
java.util.stream.*;
Â
Âclass
GFG {
   Â
public
static
void
main(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
Â
Âimport
java.util.*;
Â
Âclass
GFG {
Â
ÂÂ Â Â Â
// static function to be called
   Â
static
void
someFunction(String s)
   Â
{
       Â
System.out.println(s);
   Â
}
Â
ÂÂ Â Â Â
public
static
void
main(String[] args)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
List<String> list =
new
ArrayList<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
Â
Âimport
java.util.*;
Â
Âclass
GFG {
Â
ÂÂ Â Â Â
// instance function to be called
   Â
void
someFunction(String s)
   Â
{
       Â
System.out.println(s);
   Â
}
Â
ÂÂ Â Â Â
public
static
void
main(String[] args)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
List<String> list =
new
ArrayList<String>();
       Â
list.add(
"Geeks"
);
       Â
list.add(
"For"
);
       Â
list.add(
"GEEKS"
);
Â
ÂÂ Â Â Â Â Â Â Â
// call the instance method
       Â
// using double colon operator
       Â
list.forEach((
new
GFG())::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
Â
Âimport
java.util.*;
import
java.util.function.*;
Â
Âclass
Test {
Â
ÂÂ Â Â Â
// super function to be called
   Â
String print(String str)
   Â
{
       Â
return
(
"Hello "
+ str +
"\n"
);
   Â
}
}
Â
Âclass
GFG
extends
Test {
Â
ÂÂ Â Â Â
// 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);
Â
ÂÂ Â Â Â Â Â Â Â
return
newValue;
   Â
}
Â
ÂÂ Â Â Â
// Driver code
   Â
public
static
void
main(String[] args)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
List<String> list =
new
ArrayList<String>();
       Â
list.add(
"Geeks"
);
       Â
list.add(
"For"
);
       Â
list.add(
"GEEKS"
);
Â
ÂÂ Â Â Â Â Â Â Â
// call the instance method
       Â
// using double colon operator
       Â
list.forEach(
new
GFG()::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Â
Â
Âimport
java.util.*;Â
Â
Âclass
Test {Â
   Â
String str=
null
;
     Â
Test(String s)
   Â
{
       Â
this
.str=s;
   Â
}
   Â
// instance function to be calledÂ
   Â
void
someFunction()Â
   Â
{Â
       Â
System.out.println(
this
.str);Â
   Â
}Â
}Â
Â
Âclass
GFG {Â
Â
ÂÂ Â Â Â
public
static
void
main(String[] args)Â
   Â
{Â
Â
ÂÂ Â Â Â Â Â Â Â
List<Test> list =
new
ArrayList<Test>();Â
       Â
list.add(
new
Test(
"Geeks"
));Â
       Â
list.add(
new
Test(
"For"
));Â
       Â
list.add(
new
Test(
"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
Â
Âimport
java.util.*;
Â
Âclass
GFG {
Â
ÂÂ Â Â Â
// Class constructor
   Â
public
GFG(String s)
   Â
{
       Â
System.out.println(
"Hello "
+ s);
   Â
}
Â
ÂÂ Â Â Â
// Driver code
   Â
public
static
void
main(String[] args)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
List<String> list =
new
ArrayList<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