Why it is not allowing AppTest::makeUppercase?

The short answer is that AppTest::makeUppercase isn't valid "reference to an instance method of an arbitrary object of a particular type". AppTest::makeUppercase must implement interface Function<AppTest, String> to be valid reference.

Details:

There are 4 kinds of method references in Java:

  1. ContainingClass::staticMethodName - reference to a static method
  2. containingObject::instanceMethodName - reference to an instance method of a particular object
  3. ContainingType::methodName - reference to an instance method of an arbitrary object of a particular type
  4. ClassName::new - reference to a constructor

Every single kind of method reference requires corresponding Function interface implementation. You use as a parameter the reference to an instance method of an arbitrary object of a particular type. This kind of method reference has no explicit parameter variable in a method reference and requires implementation of the interface Function<ContainingType, String>. In other words, the type of the left operand has to be AppTest to make AppTest::makeUppercase compilable. String::toUpperCase works properly because the type of parameter and type of the instance are the same - String.

import static java.lang.System.out;

import java.util.Arrays;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;

class ReferenceSource {

    private String value;

    public ReferenceSource() {
    }

    public ReferenceSource(String value) {
        this.value = value;
    }

    public String doInstanceMethodOfParticularObject(final String value) {
        return ReferenceSource.toUpperCase(value);
    }

    public static String doStaticMethod(final String value) {
        return ReferenceSource.toUpperCase(value);
    }

    public String doInstanceMethodOfArbitraryObjectOfParticularType() {
        return ReferenceSource.toUpperCase(this.value);
    }

    private static String toUpperCase(final String value) {
        return Optional.ofNullable(value).map(String::toUpperCase).orElse("");
    }
}

public class Main {
    public static void main(String... args) {
        // #1 Ref. to a constructor
        final Supplier<ReferenceSource> refConstructor = ReferenceSource::new;
        final Function<String, ReferenceSource> refParameterizedConstructor = value -> new ReferenceSource(value);

        final ReferenceSource methodReferenceInstance = refConstructor.get();

        // #2 Ref. to an instance method of a particular object
        final UnaryOperator<String> refInstanceMethodOfParticularObject = methodReferenceInstance::doInstanceMethodOfParticularObject;

        // #3 Ref. to a static method
        final UnaryOperator<String> refStaticMethod = ReferenceSource::doStaticMethod;

        // #4 Ref. to an instance method of an arbitrary object of a particular type
        final Function<ReferenceSource, String> refInstanceMethodOfArbitraryObjectOfParticularType = ReferenceSource::doInstanceMethodOfArbitraryObjectOfParticularType;

        Arrays.stream(new String[] { "a", "b", "c" }).map(refInstanceMethodOfParticularObject).forEach(out::print);
        Arrays.stream(new String[] { "d", "e", "f" }).map(refStaticMethod).forEach(out::print);
        Arrays.stream(new String[] { "g", "h", "i" }).map(refParameterizedConstructor).map(refInstanceMethodOfArbitraryObjectOfParticularType)
                .forEach(out::print);
    }
}

Additionally, you could take a look at this and that thread.

Answer from Oleks on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › tutorial › java › javaOO › methodreferences.html
Method References (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
The BiFunction functional interface can represent a lambda expression or method reference that accepts two arguments and produces a result. The method references Person::compareByAge and MethodReferencesExamples::appendStrings are references to a static method.
Top answer
1 of 3
7

Why it is not allowing AppTest::makeUppercase?

The short answer is that AppTest::makeUppercase isn't valid "reference to an instance method of an arbitrary object of a particular type". AppTest::makeUppercase must implement interface Function<AppTest, String> to be valid reference.

Details:

There are 4 kinds of method references in Java:

  1. ContainingClass::staticMethodName - reference to a static method
  2. containingObject::instanceMethodName - reference to an instance method of a particular object
  3. ContainingType::methodName - reference to an instance method of an arbitrary object of a particular type
  4. ClassName::new - reference to a constructor

Every single kind of method reference requires corresponding Function interface implementation. You use as a parameter the reference to an instance method of an arbitrary object of a particular type. This kind of method reference has no explicit parameter variable in a method reference and requires implementation of the interface Function<ContainingType, String>. In other words, the type of the left operand has to be AppTest to make AppTest::makeUppercase compilable. String::toUpperCase works properly because the type of parameter and type of the instance are the same - String.

import static java.lang.System.out;

import java.util.Arrays;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;

class ReferenceSource {

    private String value;

    public ReferenceSource() {
    }

    public ReferenceSource(String value) {
        this.value = value;
    }

    public String doInstanceMethodOfParticularObject(final String value) {
        return ReferenceSource.toUpperCase(value);
    }

    public static String doStaticMethod(final String value) {
        return ReferenceSource.toUpperCase(value);
    }

    public String doInstanceMethodOfArbitraryObjectOfParticularType() {
        return ReferenceSource.toUpperCase(this.value);
    }

    private static String toUpperCase(final String value) {
        return Optional.ofNullable(value).map(String::toUpperCase).orElse("");
    }
}

public class Main {
    public static void main(String... args) {
        // #1 Ref. to a constructor
        final Supplier<ReferenceSource> refConstructor = ReferenceSource::new;
        final Function<String, ReferenceSource> refParameterizedConstructor = value -> new ReferenceSource(value);

        final ReferenceSource methodReferenceInstance = refConstructor.get();

        // #2 Ref. to an instance method of a particular object
        final UnaryOperator<String> refInstanceMethodOfParticularObject = methodReferenceInstance::doInstanceMethodOfParticularObject;

        // #3 Ref. to a static method
        final UnaryOperator<String> refStaticMethod = ReferenceSource::doStaticMethod;

        // #4 Ref. to an instance method of an arbitrary object of a particular type
        final Function<ReferenceSource, String> refInstanceMethodOfArbitraryObjectOfParticularType = ReferenceSource::doInstanceMethodOfArbitraryObjectOfParticularType;

        Arrays.stream(new String[] { "a", "b", "c" }).map(refInstanceMethodOfParticularObject).forEach(out::print);
        Arrays.stream(new String[] { "d", "e", "f" }).map(refStaticMethod).forEach(out::print);
        Arrays.stream(new String[] { "g", "h", "i" }).map(refParameterizedConstructor).map(refInstanceMethodOfArbitraryObjectOfParticularType)
                .forEach(out::print);
    }
}

Additionally, you could take a look at this and that thread.

2 of 3
1
String::toUpperCase

is short version of

text -> {
    return text.toUpperCase();
}

is again short version of

new Functon<String, String> (String text) {
    Override
    public String apply(String text) {
        return text.toUpperCase();
    }
}

so when you want AppTest::myMethod

you need

public class AppTest {

    public String myMethod(){
        return this.toString();
    }

    public void printFormattedString2(AppTest appTest, Function<AppTest, String> formatter){
        System.out.println(formatter.apply(appTest));
    }

    public static void main(String[] args) {
        AppTest appTest = new AppTest();

        appTest.printFormattedString2(appTest, AppTest::myMethod);
    }
}

because whole version looks so

appTest.printFormattedString2(appTest, new Function<AppTest, String>() {
    @Override
    public String apply(AppTest text) {
        return text.makeUppercase2();
    }
});
🌐
GeeksforGeeks
geeksforgeeks.org › java › static-method-in-java-with-examples
Static Method in Java With Examples - GeeksforGeeks
A static method in Java is associated with the class, not with any object or instance. It can be accessed by all instances of the class, but it does not rely on any specific instance. Static methods can access static variables directly without the need for an object. They cannot access non-static variables (instance) or methods directly. Static methods can be accessed directly in both static and non-static contexts. ... The name of the class can be used to invoke or access static methods.
Published   2 weeks ago
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-method-references
Java Method References - GeeksforGeeks
... import java.util.*; class MathUtil{ static void square(int n) { System.out.println(n * n); } } class GFG{ public static void main(String[] args) { List<Integer> list = Arrays.asList(2, 3, 4); list.forEach(MathUtil::square); } }
Published   1 month ago
🌐
TutorialsPoint
tutorialspoint.com › reference-to-a-static-method-using-method-references-in-java8
Reference to a static method using method references in ...
August 4, 2020 - The first approach is a regular approach to print the elements using a for loop. The second approach is to use a lambda expression in a for-each loop. The third approach is using a method reference in a for-each loop to print all the elements. ...
🌐
Baeldung
baeldung.com › home › java › core java › method references in java
Method References in Java | Baeldung
March 26, 2025 - Even though it’s still a one-liner, the method reference is much easier to read and understand. We can reference a constructor in the same way that we referenced a static method in our first example.
🌐
GeeksforGeeks
geeksforgeeks.org › java › static-methods-vs-instance-methods-in-java
Static Method vs Instance Method in Java - GeeksforGeeks
January 2, 2025 - Has access to the this reference, pointing to the current object · Java · import java.io.*; class Test { String n = ""; // Instance method public void test(String n) { this.n = n; } } class Geeks { public static void main(String[] args) { // create an instance of the class Test t = new Test(); // calling an instance method in the class 'Geeks' t.test("GeeksforGeeks"); System.out.println(t.n); } } Output · GeeksforGeeks · Explanation: The above example shows how to use an instance method in Java.
🌐
InfoWorld
infoworld.com › home › blogs › java 101: learn java
Get started with method references in Java | InfoWorld
November 12, 2019 - A static method reference refers to a static method in a specific class. Its syntax is className::staticMethodName, where className identifies the class and staticMethodName identifies the static method. An example is Integer::bitCount.
🌐
CodingNomads
codingnomads.com › java-method-reference-examples
Java Method Reference Examples
The following class represents a Person, and as you can see, it contains a static method - as a reminder, a static method can be called without having to create an instance of the class first.
Find elsewhere
🌐
Great Learning
mygreatlearning.com › blog › it/software development › what is static method in java with examples
What is Static Method in Java with Examples
September 12, 2024 - For example, you might want to write a method that prints the contents of a file. This method does not need to know anything about the particular file being printed, so it can be declared static.
🌐
BeginnersBook
beginnersbook.com › 2017 › 10 › method-references-in-java-8
Method References in Java 8
import java.util.Arrays; public class Example { public static void main(String[] args) { String[] stringArray = { "Steve", "Rick", "Aditya", "Negan", "Lucy", "Sansa", "Jon"}; /* Method reference to an instance method of an arbitrary * object of a particular type */ Arrays.sort(stringArray, ...
🌐
Tutorialspoint
tutorialspoint.com › java › java_method_references.htm
Java - Method References
The first approach is a regular approach to print the elements using a for loop. The second approach is to use a lambda expression in a for-each loop. The third approach is using a method reference in a for-each loop to print all the elements. ...
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › java 8 method reference (with examples)
Java 8 Method Reference (with Examples)
May 29, 2024 - An example to use Math.max() which is a static method. List<Integer> integers = Arrays.asList(1,12,433,5); Optional<Integer> max = integers.stream().reduce( Math::max ); max.ifPresent(value -> System.out.println(value)); ... This type of method ...
🌐
Scaler
scaler.com › home › topics › java › static method in java with examples
Static Method in Java With Examples - Scaler Topics
April 1, 2024 - This will cause an error, and in the case of the super keyword, we know super is the reference of the parent class very well, but we cannot use a reference in the body of the static method. We have already discussed the reason behind this. Java's main() method is static since an object doesn't need to call a static method.
🌐
Quora
quora.com › What-is-the-reason-static-methods-are-called-by-an-object-reference-instead-of-a-class-name-in-Java
What is the reason static methods are called by an object reference instead of a class name in Java? - Quora
Answer (1 of 2): Question: What is the reason static methods are called by an object reference instead of a class name in Java? Your premise is flawed. In both Java and C++ you do use a class name to call a static method or access a static field/object. See here (Java): [code]public class ABC {...
🌐
Study.com
study.com › courses › computer science courses › computer science 109: introduction to programming
Static Method in Java: Definition & Example - Lesson | Study.com
November 16, 2021 - To call a static method outside of the class that defines it we can use the class name such as Example.printMessage();. If you forgot to add the "static" keyword to your printMessage method, calling the method this way would result in a syntax error such as "cannot make a static reference to the non-static method printMessage() from the type Example." Note that using the class name for calling static methods is recommended but not mandatory. Java syntax allows calling static methods from an instance.
🌐
DZone
dzone.com › coding › languages › java lambda: method reference
Java Lambda: Method Reference
October 11, 2018 - In a method reference, you place the object (or class) that contains the method that you want to call before the delimiter :: operator and the name of the method is provided after it without arguments. ... Each of these types of method references are covered in the following sections. The following program demonstrates the static method reference.
🌐
Medium
medium.com › javarevisited › method-references-in-java8-9714496d5306
Method References in Java8. Method references provide a shorthand… | by Srikanth Dannarapu | Javarevisited | Medium
April 20, 2023 - In this example, we created a reference to the static method multiply in the MathUtils class using the MathUtils::multiply syntax. We then assigned this reference to an IntBinaryOperator variable, which is a functional interface that takes two ...
🌐
Hero Vired
herovired.com › learning-hub › topics › method-reference-in-java-8
Method Reference in Java 8: Explained with Code Examples
September 20, 2024 - A method reference in Java 8 is a shorthand notation of a lambda expression to call a method. Method references employ the symbol ‘::’ in order to distinguish the method name itself from its owner class or any other object. Such references can be made to static methods or instance methods ...
🌐
Tpoint Tech
tpointtech.com › java-8-method-reference
Java Method References - Tpoint Tech
March 17, 2025 - Hello, this is static method. In the following example, we are using predefined functional interface Runnable to refer static method.