Method References in Java

Java is a redundant language, and that can grow in the way of productivity and readability. Decreasing standardized text and monotonous code has constantly been a prevalent job with Java developers. After this, Developers usually sought clean, concise, and readable code. 

Lambda Expressions removed the need to type cumbersome boilerplate code when it comes to everyday tasks by allowing developers to call them without them belonging to a class and passing them as if they were objects.

Simultaneously with lambda expressions, Method references were introduced in the Java language in the latest version of Java, i.e., Java SE 8. Method references in Java are another absolutely valuable feature that is computed to Java 8. Lambda Expressions are just made extra compact and manageable with the help of Method references. 

Sometimes, a lambda expression seems to do nothing more significant than calling an existent method. Whereas, Method references in Java are a specialized kind of lambda expression, and they invoke (reference) the methods using a method name. 

In simple words, Java Method references are typically compressed Lambda Expressions that are used for invoking methods. Method references are used when the lambda expression is declaring a function and performing nothing more. Two Integer colons (::) indicate method reference.

Syntax of Method Reference in Java

The syntax of a Java method reference is divided into two parts. 

These are:

  • Class / Object 
  • Method / Constructor

These both parts are parted by Integer colons (::). There are no further parameters transferred with the method reference.

The syntax is:

Types of Method references in Java 

Java SE 8 allows a total of three types of method references. These are – 

  1. Static Method Reference:It is used to refer to static methods from a class (method described in a class). 
  • The syntax of static method reference is:
  • An Example of a static method reference in Java is:

Output:

  • Reference to an instance method –In this type of method reference in Java, we can refer to an instance method using a reference to the provided object and by anonymous object also. We can also refer methods by class object and unidentified object.
  • The syntax of object instance method reference is:
  • An example of an instance method reference using reference and anonymous object in Java is:

Output:

  • Reference to a Constructor:In this type of method reference in Java, we can refer to a constructor using the new keyword. This reference to a constructor can also be done with the assistance of a functional interface.
  • The syntax of a reference to a constructor in method references in Java is:
  • An example of a reference to a constructor in method references in Java is:

Output:

Important Points to remember about Method References in Java 

  • The most important advantage of the reference method in Java is that they make the code more accurate by reducing lambda expression and making the code understandable and readable.
  • There are many distinct types of method references in Java SE 8. 

These are:

  1. Static method reference
  2. An instance method using a reference object and an anonymous object
  3. Reference to a constructor
  • The double-colon operator (::) is used for the method reference in Java. This corresponding symbol is also used as a scope resolution operator in C++. However, the method references in Java and scope resolution operator in C++ have nothing to with each other. They have different uses in their respective programming languages.

Pin It on Pinterest

Share This