javagenericsmethod-reference

Syntax for specifying a method reference to a generic method


I read the following code in "Java - The beginner's guide"

interface SomeTest <T>
{
    boolean test(T n, T m);
}

class MyClass
{
    static <T> boolean myGenMeth(T x, T y)
    {
        boolean result = false;
        // ...
        return result;
    }
}

The following statement is valid

SomeTest <Integer> mRef = MyClass :: <Integer> myGenMeth;

Two points were made regarding the explanation of the above code

1 - When a generic method is specified as a method reference, its type argument comes after the :: and before the method name.

2 - In case in which a generic class is specified, the type argument follows the class name and precedes the ::.

My query:-

The code above is the example of the first quoted point

Can someone provide me an example of code which implement the second quoted point?

(Basically I don't understand the second quoted point).


Solution

  • The second quoted point just means that the type parameter belongs to the class. For example:

    class MyClass<T>
    {
        public boolean myGenMeth(T x, T y)
        {
            boolean result = false;
            // ...
            return result;
        }
    }
    

    This would then be called like this:

    SomeTest<Integer> mRef = new MyClass<Integer>() :: myGenMeth;