0% found this document useful (0 votes)
71 views

Java 8 Method Reference - Javatpoint

1. The document discusses different types of method references in Java, including references to static methods, instance methods, and constructors. 2. It provides examples of each type, showing how to refer to a method using the class name, object name, or "new" keyword depending on whether it is static, instance, or a constructor. 3. The examples demonstrate how to use method references with functional interfaces like Runnable and BiFunction to call methods more compactly than lambda expressions.

Uploaded by

Redouane Elalami
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

Java 8 Method Reference - Javatpoint

1. The document discusses different types of method references in Java, including references to static methods, instance methods, and constructors. 2. It provides examples of each type, showing how to refer to a method using the class name, object name, or "new" keyword depending on whether it is static, instance, or a constructor. 3. The examples demonstrate how to use method references with functional interfaces like Runnable and BiFunction to call methods more compactly than lambda expressions.

Uploaded by

Redouane Elalami
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Home Java Programs OOPs

Global Hiring Guide


Learn About Employment
Contracts, Team Operations,
Benefits & More. Download our
eBook.

Globalization PaDners

Open

Java Method References


Java provides a new feature called method
reference in Java 8. Method reference is
used to refer method of functional
interface. It is compact and easy form of
lambda expression. Each time when you
are using lambda expression to just
referring a method, you can replace your
lambda expression with method reference.
In this tutorial, we are explaining method
reference concept in detail.

Types of Method References

The Work-From-
Anywhere Model
Take Advantage of Remote
Employees and Work-From-
Anywhere Models to Hire Globally.

Globalization PaDners

Open

There are following types of method


references in java:

1. Reference to a static method.

2. Reference to an instance method.

3. Reference to a constructor.

1) Reference to a Static
Method

You can refer to static method defined in


the class. Following is the syntax and
example which describe the process of
referring static method in Java.

Syntax

1. ContainingClass::staticMethodName

Example 1

In the following example, we have defined


a functional interface and referring a static
method to it's functional method say().

interface Sayable{

void say();

public class MethodReference {

public static void saySomething(){

System.out.println("Hello, this is static meth

public static void main(String[] args) {

// Referring static method

Sayable sayable = MethodReference::saySo

// Calling interface method

sayable.say();

Test it Now

Output:

Hello, this is static method.

Example 2

In the following example, we are using


predefined functional interface Runnable to
refer static method.

public class MethodReference2 {

public static void ThreadStatus(){

System.out.println("Thread is running...");

public static void main(String[] args) {

Thread t2=new Thread(MethodReference2:

t2.start();

Test it Now

Output:

Thread is running...

Example 3

You can also use predefined functional


interface to refer methods. In the following
example, we are using BiFunction interface
and using it's apply() method.

import java.util.function.BiFunction;

class Arithmetic{

public static int add(int a, int b){

return a+b;

public class MethodReference3 {

public static void main(String[] args) {

BiFunction<Integer, Integer, Integer>adder = Arit

int result = adder.apply(10, 20);

System.out.println(result);

Test it Now

Output:

30

Example 4

You can also override static methods by


referring methods. In the following
example, we have defined and overloaded
three add methods.

import java.util.function.BiFunction;

class Arithmetic{

public static int add(int a, int b){

return a+b;

public static float add(int a, float b){

return a+b;

public static float add(float a, float b){

return a+b;

public class MethodReference4 {

public static void main(String[] args) {

BiFunction<Integer, Integer, Integer>adder1 = Ar

BiFunction<Integer, Float, Float>adder2 = Arithm

BiFunction<Float, Float, Float>adder3 = Arithmet

int result1 = adder1.apply(10, 20);

float result2 = adder2.apply(10, 20.0f);

float result3 = adder3.apply(10.0f, 20.0f);

System.out.println(result1);

System.out.println(result2);

System.out.println(result3);

Test it Now

Output:

30

30.0

30.0

2) Reference to an Instance
Method
like static methods, you can refer instance
methods also. In the following example,
we are describing the process of referring
the instance method.

Syntax

1. containingObject::instanceMethodName

Example 1

In the following example, we are referring


non-static methods. You can refer methods
by class object and anonymous object.

interface Sayable{

void say();

public class InstanceMethodReference {

public void saySomething(){

System.out.println("Hello, this is non-

static method.");

public static void main(String[] args) {

InstanceMethodReference methodReference

// Referring non-

static method using reference

Sayable sayable = methodReference::say

// Calling interface method

sayable.say();

// Referring non-

static method using anonymous object

Sayable sayable2 = new InstanceMethod

// Calling interface method

sayable2.say();

Test it Now

Output:

Hello, this is non-static method.

Hello, this is non-static method.

Example 2

In the following example, we are referring


instance (non-static) method. Runnable
interface contains only one abstract
method. So, we can use it as functional
interface.

public class InstanceMethodReference2 {

public void printnMsg(){

System.out.println("Hello, this is instance m

public static void main(String[] args) {

Thread t2=new Thread(new InstanceMethodR

t2.start();

Test it Now

Output:

Hello, this is instance method

Example 3

In the following example, we are using


BiFunction interface. It is a predefined
interface and contains a functional method
apply(). Here, we are referring add method
to apply method.

import java.util.function.BiFunction;

class Arithmetic{

public int add(int a, int b){

return a+b;

public class InstanceMethodReference3 {

public static void main(String[] args) {

BiFunction<Integer, Integer, Integer>adder = new

int result = adder.apply(10, 20);

System.out.println(result);

Test it Now

Output:

30

3) Reference to a Constructor
You can refer a constructor by using the
new keyword. Here, we are referring
constructor with the help of functional
interface.

Syntax

1. ClassName::new

Example

1. interface Messageable{

2. Message getMessage(String msg);

3. }

4. class Message{

5. Message(String msg){

6. System.out.print(msg);

7. }

8. }

9. public class ConstructorReference {

0. public static void main(String[] args) {

1. Messageable hello = Message::new;

2. hello.getMessage("Hello");

3. }

4. }

Test it Now

Output:

Hello

← Prev Next →

For Videos Join Our Youtube


Youtube
Channel: Join Now

Feedback

Send your Feedback to


[email protected]

Help Others, Please Share

Learn Latest Tutorials

Splunk tutorial SPSS tutorial

Splunk SPSS

Swagger tutorial T-SQL tutorial

Swagger Transact-SQL

Tumblr tutorial React tutorial

Tumblr ReactJS

Regex tutorial

Regex Reinforcement
Learning

RxJS tutorial

R Programming RxJS

React Native Python Design


Patterns

Python Pillow Python Turtle

Keras tutorial

Keras

Preparation

Aptitude

Aptitude Reasoning

Verbal Ability

Verbal Ability Interview


Questions

Company
Questions
Trending Technologies

AWS Tutorial

Artificial AWS
Intelligence

Selenium tutorial

Selenium Cloud Computing

Hadoop tutorial ReactJS Tutorial

Hadoop ReactJS

Data Science Angular 7

Git Tutorial

Blockchain Git

DevOps Tutorial

Machine Learning DevOps

B.Tech / MCA

DBMS tutorial

DBMS Data Structures

DAA tutorial

DAA Operating System

Computer Network Compiler Design

Computer Discrete
Organization Mathematics

Ethical Hacking Computer Graphics

html tutorial

Software Web Technology


Engineering

Automata Tutorial

Cyber Security Automata

C++ tutorial

C Programming C++

Java tutorial

Java .Net

Python tutorial List of Programs

Python Programs

Control System Data Mining

Data Warehouse

⇧ SCROLL TO TOP

You might also like