What is Method References?
It is a feature which is related to Lambda Expression. It allows us to reference constructors or methods without executing them. Method references and Lambda are similar in that they both require a target type that consist of a compatible functional interface.
Types of Functional Interface
There are four types of method reference, this is summarized below.
Type Example Syntax
- Reference to a static method - Example: ContainingClass::staticMethodName Syntax: Class::staticMethodName
- Reference to a constructor - Example: ClassName::new Syntax: ClassName::new
- Reference to an instance method of an arbitrary object of a particular type - Example: ContainingType::methodName Syntax: Class::instanceMethodName
- Reference to an instance method of a particular object - Example: containingObject::instanceMethodName Syntax:object::instanceMethodName
I will explain further about the four types of method referenced in the table.