Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In this quick article, we’ll discuss the double colon operator ( :: ) in Java 8 and go over the scenarios where the operator can be used.

Further reading:

Java 8 Interview Questions(+ Answers)

A set of popular Java8-related interview questions and of course answers.

Guide To Java Optional

Quick and practical guide to the Java Optional class

New Features in Java 8

A short intro into the new features of Java 8; the focus is on default and static interface methods, static method references, and Optional.

2. From Lambdas to Double Colon Operator

With Lambdas expressions, we’ve seen that code can become very concise.

For example, to create a comparator, the following syntax is enough:

Comparator c = (Computer c1, Computer c2) -> c1.getAge().compareTo(c2.getAge());

Then, with type inference:

Comparator c = (c1, c2) -> c1.getAge().compareTo(c2.getAge());

But can we make the code above even more expressive and readable? Let’s have a look:

Comparator c = Comparator.comparing(Computer::getAge);

We’ve used the :: operator as shorthand for lambdas calling a specific method – by name. And the end, result is of course even more readable syntax.

3. How Does It Work?

Very simply put, when we are using a method reference – the target reference is placed before the delimiter :: and the name of the method is provided after it.

For example:

Computer::getAge;

We’re looking at a method reference to the method getAge defined in the Computer class.

We can then operate with that function:

Function<Computer, Integer> getAge = Computer::getAge;
Integer computerAge = getAge.apply(c1);

Notice that we’re referencing the function – and then applying it to the right kind of argument.

4. Method References

We can make good use of this operator in quite some scenarios.

4.1. A Static Method

First, we’re going to make use of a static utility method:

List inventory = Arrays.asList(
  new Computer( 2015, "white", 35), new Computer(2009, "black", 65));
inventory.forEach(ComputerUtils::repair);

4.2. An Instance Method of an Existing Object

Next, let’s have a look at an interesting scenario – referencing a method of an existing object instance.

We’re going to use the variable System.out – an object of type PrintStream which supports the print method:

Computer c1 = new Computer(2015, "white");
Computer c2 = new Computer(2009, "black");
Computer c3 = new Computer(2014, "black");
Arrays.asList(c1, c2, c3).forEach(System.out::print);

4.3. An Instance Method of an Arbitrary Object of a Particular Type

Computer c1 = new Computer(2015, "white", 100);
Computer c2 = new MacbookPro(2009, "black", 100);
List inventory = Arrays.asList(c1, c2);
inventory.forEach(Computer::turnOnPc);

As you can see, we’re referencing the turnOnPc method not on a specific instance, but on the type itself.

At line 4 the instance method turnOnPc will be called for every object of inventory.

And this naturally means that – for c1 the method turnOnPc will be called on the Computer instance and for c2 on MacbookPro instance.

4.4. A Super Method of a Particular Object

Suppose you have the following method in the Computer superclass:

public Double calculateValue(Double initialValue) {
    return initialValue/1.50;
}

and this one in MacbookPro subclass:

@Override
public Double calculateValue(Double initialValue){
    Function<Double, Double> function = super::calculateValue;
    Double pcValue = function.apply(initialValue);
    return pcValue + (initialValue/10) ;
}

A call to calculateValue method on a MacbookPro instance:

macbookPro.calculateValue(999.99);

will also produce also a call to calculateValue on the Computer superclass.

5. Constructor References

5.1. Create a New Instance

Referencing a constructor to instantiate an object can be quite simple:

@FunctionalInterface
public interface InterfaceComputer {
    Computer create();
}

InterfaceComputer c = Computer::new;
Computer computer = c.create();

What if you have two parameters in a constructor?

BiFunction<Integer, String, Computer> c4Function = Computer::new; 
Computer c4 = c4Function.apply(2013, "white");

If parameters are three or more you have to define a new Functional interface:

@FunctionalInterface 
interface TriFunction<A, B, C, R> { 
    R apply(A a, B b, C c); 
    default <V> TriFunction<A, B, C, V> andThen( Function<? super R, ? extends V> after) { 
        Objects.requireNonNull(after); 
        return (A a, B b, C c) -> after.apply(apply(a, b, c)); 
    } 
}

Then, initialize your object:

TriFunction <Integer, String, Integer, Computer> c6Function = Computer::new;
Computer c3 = c6Function.apply(2008, "black", 90);

5.2. Create an Array

Finally, let’s see how to create an array of Computer objects with five elements:

Function <Integer, Computer[]> computerCreator = Computer[]::new;
Computer[] computerArray = computerCreator.apply(5);

6. Conclusion

As we’re starting to see, the double colon operator – introduced in Java 8 – will be very useful in some scenarios, and especially in conjunction with Streams.

It’s also quite important to have a look at functional interfaces for a better understanding of what happens behind the scenes.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW