Double Colon Operator (::) in Java
In Java, we can use the double colon operator (::
) to call a method by referring to it with the help of its class name.
We can also do this by using the lambda expressions. The only difference we find here is that we can directly reference the method by name while using the ::
operator.
Whereas, when we use lambda expressions, we need a delegate to the method we need to call. This article will discuss using the ::
operator in Java.
There are various places where we can use the ::
operator in Java. We can use the ::
operator for referencing methods.
Hence, we can extract static methods from classes or objects using the ::
. We can even use the ::
operator for constructors.
Also, when we are given an input stream, we can use the ::
operator to print the elements of the input stream.
When and How to Use the Double Colon (::
) Operator in Java
We can use the ::
operator while dealing with static methods, instance methods, super methods, instance method of an arbitrary object of a particular type, and even class constructors.
Let us discuss each of the approaches one by one.
While Dealing With Static Methods
We can use the ::
operator in Java when dealing with static methods. To get a reference to the static method of a class, we will first write the class name followed by the ::
operator, and then we will write the method name.
Let us look at the syntax of this approach:
Syntax:
(ClassName::methodName)
Let us now look at the code below to understand how it works.
import java.util.*;
public class Main {
static void staticFunction(String s) // static function which is called using :: operator
{
System.out.println(s);
}
public static void main(String args[]) {
List<String> list = new ArrayList<String>();
list.add("This");
list.add("is");
list.add("an");
list.add("example");
list.forEach(Main::staticFunction);
}
}
Output:
This
is
an
example
While Dealing With Instance Methods
We can also use the ::
operator in Java when dealing with instance methods.
To get a reference to the instance method of a class, we will first write the object of the class. Then we will put the ::
operator, and finally, we will write the method name.
Let us look at the syntax of this approach.
Syntax:
(ObjectOfTheClass::methodName)
Let us now look at the code below to understand how it works.
import java.util.*;
public class Main {
void instanceFunction(String s) // function which is called using :: operator
{
System.out.println(s);
}
public static void main(String args[]) {
List<String> list = new ArrayList<String>();
list.add("This");
list.add("is");
list.add("an");
list.add("example");
Main object = new Main();
list.forEach(object::instanceFunction);
}
}
Output:
This
is
an
example
While Dealing With Super Methods
We can also use the ::
operator in Java when dealing with super methods.
To reference the super method of a class, we will first write the keyword super
. Then we will put the ::
operator, and finally, we will write the method name.
Let us look at the syntax of this approach.
Syntax:
(super::methodName)
Let us now look at the code below to understand how it works.
// Java code to show use of double colon operator for super methods
import java.util.*;
import java.util.function.*;
class SuperClass {
// super function to be called
String printFunction(String s) {
return ("This is Print function output from the SuperClass: " + s);
}
}
public class Main extends SuperClass {
// instance method to override super method
@Override
String printFunction(String s) {
// call the super method
// using double colon operator
Function<String, String> func = super::printFunction;
String newValue = func.apply(s);
System.out.println(newValue);
return newValue;
}
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("This");
list.add("is");
list.add("an");
list.add("example");
Main object = new Main();
list.forEach(object::printFunction);
}
}
Output:
This is Print function output from the SuperClass: This
This is Print function output from the SuperClass: is
This is Print function output from the SuperClass: an
This is Print function output from the SuperClass: example
While Dealing With the Instance Method of an Arbitrary Object of a Particular Type
We can also use the ::
operator in Java when dealing with the instance method of an arbitrary object of a particular type.
To reference the instance method of an arbitrary object of a specific type, we will first write the class name. Then we will put the ::
operator, and finally, we will write the method name.
Let us look at the syntax of this approach.
Syntax:
(ClassName::methodName)
Let us now look at the code below to understand how it works.
// Java code to show the use of double colon operator for instance method of arbitrary type
import java.util.*;
class Test {
String s = null;
Test(String s) {
this.s = s;
}
// instance function to be called
void instanceFunction() {
System.out.println(this.s);
}
}
public class Main {
public static void main(String[] args) {
List<Test> list = new ArrayList<Test>();
list.add(new Test("This"));
list.add(new Test("is"));
list.add(new Test("an"));
list.add(new Test("example"));
list.forEach(Test::instanceFunction); // call the instance method using double colon operator
}
}
Output:
This
is
an
example
While Dealing With Class Constructor
We can also use the ::
operator in Java when dealing with the class constructor.
To get a reference to the class constructor, we will first write the name of the class. Then we will put the ::
operator, and finally, we will write new
which will call the constructor of that particular class.
Let us look at the syntax of this approach.
Syntax:
(ClassName::new)
Let us now look at the code below to understand how it works.
// Java code to show the use of double colon operator for class constructor
import java.util.*;
public class Main {
public Main(String s) {
System.out.println(s);
}
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("This");
list.add("is");
list.add("an");
list.add("example");
list.forEach(Main::new); // call the instance method using double colon operator
}
}
Output:
This
is
an
example
Conclusion
In this article, We looked at five places and used the double colon operator (::
) in Java.
All of the techniques have their significance, and you can apply the ::
operator whenever you come across any of the above situations. Be careful with the syntax, as improper use might result in an erroneous situation.