Java Reference
In-Depth Information
The method takes no argument and returns a List<Person> . A Supplier<T> represents a function that
takes no argument and returns a result of type T . The following snippet of code uses the method reference
Person::getPersons as a Supplier<List<Person>> :
Supplier<List<Person>>supplier = Person::getPersons ;
List<Person> personList = supplier.get();
FunctionUtil.forEach(personList, p -> System.out.println(p));
John Jacobs, MALE, 1975-01-20
Wally Inman, MALE, 1965-09-12
Donna Duncan, FEMALE, 1970-09-12
Instance Method References
An instance method is invoked on an object's reference. The object reference on which an instance method is invoked
is known as the receiver of the method invocation. The receiver of a method invocation can be an object reference or
an expression that evaluates to an object's reference. The following snippet of code shows the receiver of the length()
instance method of the String class:
String name = "Kannan";
// name is the receiver of the length() method
int len1 = name.length();
// "Hello" is the receiver of the length() method
int len2 = "Hello".length();
// (new String("Kannan")) is the receiver of the length() method
int len3 = (new String("Kannan")).length();
In a method reference for an instance method, you can specify the receiver of the method invocation explicitly or
you can provide it implicitly when the method is invoked. The former is called a bound receiver and the latter is called
an unbound receiver . The syntax for an instance method reference supports two variants:
objectRef::instanceMethod
ClassName::instanceMethod
Bound Receiver
For a bound receiver, use the objectRef.instanceMethod syntax. Consider the following snippet of code:
Supplier<Integer> supplier = () -> "Ellen".length() ;
System.out.println(supplier.get());
5
 
Search WWH ::




Custom Search