In Java, a method reference is a shorthand syntax for referring to an existing method by its name. It provides a way to pass a method as an argument to a function, without actually invoking the method.
There are four types of method references in Java:
- Reference to a static method: This type of method reference is used to reference a static method of a class. For example, if there is a static method
myMethod()
in a classMyClass
, we can refer to it using the following syntax:MyClass::myMethod
. - Reference to an instance method of an object: This type of method reference is used to reference an instance method of an object. For example, if there is an instance method
myMethod()
in a classMyClass
, we can refer to it using the following syntax:myObject::myMethod
, wheremyObject
is an instance ofMyClass
. - Reference to an instance method of a particular object: This type of method reference is used to reference an instance method of a specific object. For example, if there is an instance method
myMethod()
in a classMyClass
, and we have an instance ofMyClass
calledmyObject
, we can refer to it using the following syntax:myObject::myMethod
. - Reference to a constructor: This type of method reference is used to reference a constructor of a class. For example, if there is a constructor
MyClass()
in a classMyClass
, we can refer to it using the following syntax:MyClass::new
.
Method references can be used in conjunction with functional interfaces to provide a more concise syntax for lambda expressions. They can also improve the readability of code by making it more declarative and expressive.
Types of Method References:
In Java, there are four types of method references that can be used:
- Reference to a static method: A reference to a static method is used when you want to refer to a static method of a class. The syntax for this type of method reference is
ClassName::methodName
. For example,Integer::parseInt
is a reference to the static methodparseInt
of theInteger
class. - Reference to an instance method of an object: A reference to an instance method of an object is used when you want to refer to an instance method of an object. The syntax for this type of method reference is
objectName::methodName
. For example,str::toUpperCase
is a reference to the instance methodtoUpperCase
of theString
objectstr
. - Reference to an instance method of a particular object: A reference to an instance method of a particular object is used when you want to refer to an instance method of a specific object. The syntax for this type of method reference is
ClassName::methodName
. For example,System.out::println
is a reference to the instance methodprintln
of thePrintStream
objectout
of theSystem
class. - Reference to a constructor: A reference to a constructor is used when you want to refer to a constructor of a class. The syntax for this type of method reference is
ClassName::new
. For example,ArrayList::new
is a reference to the constructor of theArrayList
class.
Method references can be used in functional interfaces to provide a more concise syntax for lambda expressions. They can also improve the readability of code by making it more declarative and expressive.