This tutorial will illustrate what does “::” mean and how to use it in Java.
What Does “::” Mean in Java?
Double colon “::” is a method reference operator used to call the method. It refers to the specified method with the help of the class name. This operator behaves similar to the Lambda Expression. However, it does not need method arguments for references. That’s why “::” is simple and more effective.
Now, let’s examine the below example to check the basic working of the mentioned operator.
Example
First, we will create a String type list for numbers named “num”:
Then, add the required elements in the created list using the “add()” method:
num.add("Two");
num.add("Three");
num.add("Four");
num.add("Five");
Lastly, utilize the “forEach” loop, and print out the list elements. To do so, we will use the “println()” method of the “System” class and access it using the “::” operator:
All of the added elements will be printed out on the console:
Now, let’s check out the advanced usage of the “::” operator in Java.
How to Use “::” in Java?
The Method reference operator “::” is used to call different types of methods in Java, such as:
We will now discuss each of the mentioned methods one by one.
Method 1: Call Static Method Using “::” Operator in Java
To call the static method of a class, use the “::” operator as follows:
Syntax
In the above-given syntax, mention the target class from where the method is going to be accessed.
Example
We will use the same list created in the above example. However, we will create a static method named “display()” to print the list elements:
System.out.println(stng);
}
In the main() method, we will call the static method “display()” of the “Example” class by using the “::” method reference operator:
Output
Want to access a constructor using the method reference operator? Check out the below-given section.
Method 2: Call Constructor Using “::” Operator in Java
You can also utilize the method reference operator to invoke a class constructor.
Syntax
Example
In the constructor of the “Example” class, we will call the “System.out.println()” method to print out the added list elements of String type:
System.out.println(stng);
}
In the main() method, we call the constructor by using a “new” keyword and adding the “::” operator after specifying the class name. The “new” keyword will create a class object that automatically calls the constructor method:
Output
Let’s head towards the next method!
Method 3: Call Instance Method Using “::” Operator in Java
Similar to the static method, you can also call the instance method of the class by using the “::” operator. For this, first, you will create a class object and then call the instance method with the help of the following syntax.
Syntax
Example
We will now create an object “ex” of the “Example” class:
Then, call the instance method “display()” using the class object and “::” method reference operator:
Output
We compiled all the necessary information related to method reference operator “::” and its usage in Java.
Conclusion
The “::” is an operator called “Method Reference Operator” that is utilized to access the method by pointing to it using the class name. This type of Java operator can be utilized to call different types of methods in a program, such as Static methods, Constructors, and Instance methods. This tutorial illustrated what “::” (double colons) means and how to use it in Java.