EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Java 8 Tutorial Java 8 Method Reference
Secondary Sidebar
Java 8 Tutorial
  • Java 8 basic
    • Java 8 forEach
    • Java 8 Documentation
    • Java 8 Method Reference
    • Java 8 List to Map
    • Java 8 Parallel Stream
    • Java 8 Functional Interface
    • Java 8 API
    • Java 8 Lambda
    • Java 8 Comparator
    • Java 8 Group By
    • Java 8 Predicate
    • Java 8 Default Methods
    • Java 8 Thread
    • Java 8 HashMap
    • Java 8 Read File
    • Java 8 OpenJDK
    • Java 8 Stream Filter
    • Java 8 Interface

Java 8 Method Reference

Definition

Java 8 method reference is used to refer to the functional method interface. It’s a very easy and compact form of the lambda expression. When we use the lambda expression for referring the method, we can replace our lambda expression by using method reference. Lambda function and functional interfaces are required to grasp the references in java. As we know, the method is statements collection, which performs periodical tasks.

Introduction to Java 8 Method Reference

It performs some specified task; while performing the task, it will not return anything. Java 8 method allows us to use the code without retyping the same. We are using the method as it will contain the primitive values or objects and treat them as a variable. We are using lambda expression for creating the method, which was anonymous.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The lambda expression will call the existing method. Method reference is a new feature that was introduced in java 8; it will contain the short and easily readable syntax of the lambda expression. We are using the method reference to refer to the method of a functional interface. We can also replace the reference of the method for our lambda expression at the time we are referring the lambda expression with the method.

Java 8 Method Reference

Key Takeaways

  • Java 8 method reference implements the functional interface, which contains less code as compared to the lambda expressions.
  • By using it, we do not provide the functional interface implementation. Instead of that, we are providing the reference of a method that already exists.

Syntax of Java 8 Method Reference

The syntax will be different for every method. We are providing it by using three ways. Below is the syntax as per the types of reference methods as follows:

1. Reference to static method

Below is the syntax of reference to static method as follows. This syntax will contain the class name and name of the static method.

Syntax:

ContainingClass::StaticMethodName

2. Reference to instance method

Below is the syntax of reference to instance method as follows. This syntax contains the object name and the name of the instance method.

Syntax:

ContainingObject::InstanceMethodName

3. Reference to a constructor

Below is the syntax of reference to a constructor as follows. This syntax will contain the class name and name of the new keyword.

Syntax:

ContainingClass::new

Types of Java 8 Method Reference with Example

Below are the types, it contains four types as follows:

1. Static Method Reference

We can refer to the static method, which defines a class. By using static method reference, it is presented in the class will refer to the specified class.

In the below example, we are defining the interface name as functional and referring to the method as static as follows.

Code: 

interface stat {
void sta ();
}
public class statref {
public static void statRefrence (){
System.out.println ("static method");
}
public static void main(String[] args){
stat st = statref::statRefrence;
st.sta ();
}
}

Output:

Java 8 Method Reference Interference name

In the below example, we are using a runnable interface, which was predefined and was used for referring to the static method.

Code:

public class statRef {
public static void ststat () {
System.out.println ("Static reference method");
}
public static void main(String[] args) {
Thread st=new Thread (statRef::ststat);
st.start ();
}
}

Output:

Java 8 Method Reference - Static method

2. Instance Method Reference

In reference to methods like the static method, we are also referring to the instance method. The below example shows the instant method reference as follows. In the below example, we are referring to the instance method.

Code:

public class InstMethod {
public void msg () {
System.out.println ("Instance method");
}
public static void main(String[] args){
Thread st = new Thread (new InstMethod ()::msg);
st.start ();
}
}

Output:

Instance method

In the below example, we refer to the instance method. We are referring to using class and anonymous objects.

Code:

interface meth {
void inst ();
}
public class InstMeth {
public void instMe (){
System.out.println ("Insance method.");
}
public static void main(String[] args){
InstMeth methodReference = new InstMeth ();
meth Meth = methodReference::instMe;
meth.inst ();
meth meth1 = new InstMeth()::instMe;
meth.inst ();
}
}

Output:

Class and anonymous object

3. Reference to a Constructor

We are referring to the constructor by using a new keyword. In the below example, we refer to the constructor by using the help of the functional interface.

Code:

interface conInt {
conMsg getMessage (String coninst);
}
class conMsg {
conMsg (String coninst) {
System.out.print (coninst);
}
}
public class ConstructorReference {
public static void main(String[] args){
conInt st = conMsg::new;
st.getMessage ("Constructor interface");
}
}

Output:

Java 8 Method Reference Constructor

4. Instance Method for an Arbitrary Object of Particular Type

We can also define the method reference by using a particular type of arbitrary object of an instance method. Below is the example of a particular type of arbitrary object of instance method as follows.

Code:

import java.util.Arrays;
public class InstMeth {
public static void main(String[] args){
String[] stringArray = { "ABC", "PQR", "XYZ", "MNP", "BCD"};
Arrays.sort (stringArray, String::compareToIgnoreCase);
for (String str: stringArray) {
System.out.println (str);
}
}
}

Output:

arbitrary object

FAQs

Given below are the FAQs mentioned:

Q1. What is the use of the method reference?

Answer: Basically, it is used to refer to the method of a particular functional interface. We can also use the method reference in the lambda expression.

Q2. How many types of Java 8 method references are available?

Answer: There are four types available.

  • Static reference method
  • Instance reference method
  • Reference to constructor
  • Instance method for an arbitrary object of the particular type

Q3. What is the syntax of the Java 8 static reference method?

Answer: Below is the syntax of the Java 8 static reference method. It contains the class name and static method name as follows.

ContainingClass::StaticMethodName

Q4. What is the main difference between java 8 static and instance method reference?

Answer: Static method contains the class name, while the instance reference method contains the object name.

Conclusion

The java 8 method reference performs some specified task; while performing the task, it will not return anything. Java 8 method allows us to use the code without retyping the same. Java 8 method reference is used to refer to the functional method interface. It’s a very easy and compact form of the lambda expression.

Recommended Articles

This is a guide to Java 8 Method Reference. Here we discuss the introduction and types of java 8 method references with examples. You may also have a look at the following articles to learn more –

  1. Text File in Java
  2. Java 8 forEach
  3. Java for Automation Testing
  4. Java SFTP
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more