Core Java

What does :: mean in Java?

Hello. In this tutorial, we will understand the method references (double colon (::) operator) in the java programming language.

1. Introduction

In java method references also called double colon operator is used to call a method by referring to it with the help of the class name or the object. The method references behave similarly to lambda expressions. The method reference in java is represented by the following syntax.

className :: methodName

A method reference can be used in four ways –

  • A method reference to a static method
  • A method reference to an instance method of an object
  • A method reference to instance methods of an arbitrary object of a particular type
  • A method reference to a constructor

2. Practice

Let us dive into some practice stuff from here and I am assuming that you already have Java 1.8 or greater installed on your local machine. I am using JetBrains IntelliJ IDEA as my preferred IDE. You’re free to choose the IDE of your choice.

2.1 Understanding method reference to a static method

Create a java file that will show the implementation of calling a method reference to a static method.

Implementation class

package com.learning;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

class SomeClass {

	public static void log(List<String> items) {
		items.forEach(e -> System.out.println(e));
	}
}

public class Example1 {

	// method reference to a static method
	public static void main(String[] args) {
		List<String> items = Arrays.asList("physics", "chemistry", "maths", "zoology", "biology");

		// referencing method to List<String> type consumer interface
		Consumer<List<String>> consumer1 = SomeClass::log;
		consumer1.accept(items);	// calling consumer method
	}
}

Run the file as a java file and if everything goes well the following logs will be shown on the IDE console.

Fig. 1: Console output

2.2 Understanding method reference to an instance method of an object

Create a java file that will show the implementation of calling a method reference to an instance method of an object.

Implementation class

package com.learning;

import java.util.Random;
import java.util.function.Supplier;

class SomeClass2 {
	
	public int getRandomResult() {
		return new Random().nextInt(1000);
	}
}

public class Example2 {

	// method reference to an instance method
	public static void main(String[] args) {
		SomeClass2 object = new SomeClass2();
		
		// reference method to Integer type supplier interface
		Supplier<Integer> supplier = object::getRandomResult;
		System.out.println(supplier.get());		// calling supplier method
	}
}

Run the file as a java file and if everything goes well the following logs will be shown on the IDE console.

Fig. 2: Console output

2.3 Understanding method reference to an instance method of an arbitrary object of a particular type

Create a java file that will show the implementation of calling a method reference to an instance method of an arbitrary object of a particular type.

Implementation class

package com.learning;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

class PersonV1 {
	UUID id;
	String name;
	
	public PersonV1(UUID pid, String pname) {
		this.id = pid;
		this.name = pname;
	}
	
	public UUID getId() {
		return id;
	}
	
	public String getName() {
		return name;
	}
	
	public void print() {
		System.out.println("person id= " + this.id + ", person name= " + this.name);
	}
}

public class Example3 {

	public static void main(String[] args) {
		List<PersonV1> persons = Arrays.asList(
				new PersonV1(UUID.randomUUID(), "Person1"),
				new PersonV1(UUID.randomUUID(), "Person2"),
				new PersonV1(UUID.randomUUID(), "Person3"),
				new PersonV1(UUID.randomUUID(), "Person4"));
		
		persons.forEach(PersonV1::print);
	}
}

Run the file as a java file and if everything goes well the following logs will be shown on the IDE console.

Fig. 3: Console output

2.4 Understanding method reference on a constructor

Create a java file that will show the implementation of calling a method reference on a constructor.

Implementation class

package com.learning;

import java.util.function.Function;

class Subject {
	
	String name;
	
	public Subject(String sname) {
		this.name = sname;
	}
	
	public String getName() {
		return name;
	}
}

public class Example4 {

	// method reference on a constructor
	public static void main(String[] args) {
		Function<String, Subject> function = (subject) -> new Subject(subject);
		
		String subjectName = function.apply("maths").getName();
		
		System.out.println(subjectName);
	}
}

Run the file as a java file and if everything goes well the following logs will be shown on the IDE console.

Fig. 4: Console output

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we discussed the practical implementation of method references and their different usages in the java programming language. You can download the source code from the Downloads section.

4. Download the Project

This was a tutorial to understand the method reference in the java programming language.

Download
You can download the full source code of this example here: What does :: mean in Java?

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button