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 it uses direct reference to the method by name instead of providing a delegate to the method.

Double colon operator can be used to refer :

  • static method (ClassName::methodName)
  • instance method (objectOfClass::methodName)
  • constructor (ClassName::new)
  • instance method (but not static) of an arbitrary object of different type (SomeClass::someInstanceMethod)
  • super method of a particular object (super::someSuperClassMethod)

Example showing reference to static method :

public class ReferenceToStaticMethod {
static void someFunction(String s) {
System.out.println(s);
}

public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Geeks");
list.add("For");
list.add("Geeks");

list.forEach(ReferenceToStaticMethod::someFunction);
}
}

Example showing reference to instance method :

public class ReferenceToInstanceMethod {
void someFunction(String s) {
System.out.println(s);
}

public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Geeks");
list.add("For");
list.add("Geeks");

list.forEach(new ReferenceToInstanceMethod()::someFunction);
}
}

Example showing reference to constructor :

public class ReferenceToConstructor {

public ReferenceToConstructor(String s) {
System.out.println("Hello " + s);
}

public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Geeks");
list.add("For");
list.add("Geeks");

list.forEach(ReferenceToConstructor::new);
}
}

Example showing reference to instance method of an arbitrary object of different type :

class TestInstanceMethod {
String s = null;

public TestInstanceMethod(String s) {
this.s = s;
}

void someFunction() {
System.out.println(this.s);
}
}

public class ReferenceToAnotherClassInstanceMethod {
public static void main(String[] args) {
List<TestInstanceMethod> list = new ArrayList<TestInstanceMethod>();
list.add(new TestInstanceMethod("Geeks"));
list.add(new TestInstanceMethod("For"));
list.add(new TestInstanceMethod("GEEKS"));

list.forEach(TestInstanceMethod::someFunction);
}
}

Example showing reference to method of super class :

class TestSuperClassMethod {
String print(String s) {
return new StringBuilder("Hello ").append(s).append("\n").toString();
}
}

public class ReferenceToSuperClassMethod extends TestSuperClassMethod {
@Override
String print(String s) {
Function<String, String> func = super::print;
String newValue = func.apply(s);
newValue +="Bye " + s + "\n";
System.out.println(newValue);

return newValue;
}

public static void main(String[] args) {
ReferenceToSuperClassMethod referenceToSuperClassMethod
= new ReferenceToSuperClassMethod();

List<String> list = new ArrayList<String>();
list.add("Geeks");
list.add("For");
list.add("GEEKS");

list.forEach(referenceToSuperClassMethod::print);
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *