Java Language – 75 – Method References

Java 8 Features – Method References
Introduction to Method References


Java 8 introduced several features that improved code readability and flexibility. One of these features is method references. Method references provide a way to simplify lambda expressions when the lambda body calls an existing method. They enhance the code’s readability and maintainability.

Types of Method References

There are several types of method references in Java 8: 1. Reference to a static method: It refers to a static method using the class name. 2. Reference to an instance method of a particular object: It refers to an instance method of a particular object. 3. Reference to an instance method of an arbitrary object of a particular type: It refers to an instance method of a particular object of a specific type. 4. Reference to a constructor: It refers to a constructor.

Using Method References

Here’s an example of using method references to sort a list of strings alphabetically:


import java.util.ArrayList;
import java.util.List;

public class MethodReferencesExample {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>();
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");

        // Using method reference to call the compareTo method
        names.sort(String::compareTo);

        // Print sorted names
        names.forEach(System.out::println);
    }
}


In this example, we use the String::compareTo method reference to simplify the sorting process. It refers to the compareTo method of the String class.

Reference to a Static Method

A common use case of method references is referencing static methods. Here’s an example:


import java.util.function.Function;

public class StaticMethodReferenceExample {
    public static void main(String[] args) {
        Function<String, Integer> parser = Integer::parseInt;
        int result = parser.apply("42");
        System.out.println("Parsed Integer: " + result);
    }
}


In this example, we use the Integer::parseInt method reference to create a Function that parses a string into an integer.

Reference to an Instance Method of a Particular Object

Method references can also refer to instance methods of specific objects. Here’s an example:


class MyClass {
    public void instanceMethod() {
        System.out.println("Instance method called");
    }
}

public class InstanceMethodReferenceExample {
    public static void main(String[] args) {
        MyClass myObject = new MyClass();
        Runnable runner = myObject::instanceMethod;
        new Thread(runner).start();
    }
}


In this example, we reference the instanceMethod of a particular MyClass object, and we use it as a Runnable to start a new thread.

Reference to an Instance Method of an Arbitrary Object of a Particular Type

You can also reference instance methods of arbitrary objects of a specific type. Here’s an example:


import java.util.function.Function;

public class ArbitraryObjectMethodReferenceExample {
    public static void main(String[] args) {
        Function<String, String> converter = String::toUpperCase;
        String result = converter.apply("hello");
        System.out.println("Converted String: " + result);
    }
}


In this example, we reference the toUpperCase method of an arbitrary String object of a particular type. This method converts the string to uppercase.

Reference to a Constructor

Method references can be used to reference constructors as well. Here’s an example:


import java.util.function.Supplier;

class Person {
    private String name;

    public Person() {
        name = "Unknown";
    }

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

public class ConstructorReferenceExample {
    public static void main(String[] args) {
        Supplier<Person> personSupplier = Person::new;
        Person person = personSupplier.get();
        System.out.println("Person's name: " + person.getName());
    }
}


In this example, we reference the default constructor of the Person class using Person::new, and we use it to create a new Person object.

Conclusion

Method references in Java 8 simplify code by allowing you to refer to methods or constructors using a concise syntax. They improve code readability and maintainability by reducing the need for lambda expressions. Whether referencing static methods, instance methods of specific objects, or constructors, method references are a powerful feature for Java developers.