18
A quick Update on Method References Parth Joshi Techno-Entrepreneur | Trainer Linkedin | http://www.parthjoshi.in

A quick tutorial to Method references

Embed Size (px)

Citation preview

A quick Update on Method References

Parth Joshi

Techno-Entrepreneur | Trainer

Linkedin | http://www.parthjoshi.in

Method References

• We can pass the reference of the method to get invoked as a callback in Java 8 using :: operator

Part of Online video tutorial series: Upgrade to Java 8

Method References

• We can use this for:

1. Static methods of a class

2. Instance methods

3. Constructor calls

Part of Online video tutorial series: Upgrade to Java 8

Method References – Static Methods

class Dog {

// static method

public static void barks() {

System.out.println("Bhow bhow");

}

public void doggyThing (Doggy doggy){

doggy.doDoggyThing();

}

}

interface Doggy {

public void doDoggyThing();

}

Suppose:Our objective is to pass some behavior in the doggyThing function

Part of Online video tutorial series: Upgrade to Java 8

Method References – Static Methods

By our Old School Method of anonymous inner class method:

// using old school anonymous inner class

Dog dog1 = new Dog();

dog.doggyThing(new Doggy() {

@Override

public void doDoggyThing() {

System.out.println("Bang Bang");

}

});

Part of Online video tutorial series: Upgrade to Java 8

Method References – Static Methods

class Dog {

// static method

public static void barks() {

System.out.println("Bhow bhow");

}

public void doggyThing (Doggy doggy){

doggy.doDoggyThing();

}

}

interface Doggy {

public void doDoggyThing();

}

Using Method Reference:We will pass body of barks() functions as behavior to doDoggyThing() of Doggy Functional Interface.

Part of Online video tutorial series: Upgrade to Java 8

Method References – Static Methods

• By using static Method reference feature of Java 8:

Dog dog = new Dog();

dog.doggyThing(Dog::barks);

Part of Online video tutorial series: Upgrade to Java 8

Method References – Static Methods

• By using static Method reference feature of Java 8:

Dog dog = new Dog();

dog.doggyThing(Dog::barks);

public static void barks() {

System.out.println("Bhow bhow");

}

interface Doggy {

public void doDoggyThing();

}

public void doggyThing (Doggy doggy){

doggy.doDoggyThing();

}Part of Online video tutorial series: Upgrade to Java 8

Method References – Static Methods

• Same operation by using Lambda Expression

// using new Lambda Expression

Dog dog2 = new Dog();

dog.doggyThing(()->{System.out.println("New Bhow");});

Part of Online video tutorial series: Upgrade to Java 8

Method References – Static Methods

• Comparison of anonymous inner class, lambda and method reference

// 2. using new Lambda Expression

dog.doggyThing(()->{System.out.println("New Bhow");});

// 3. using method reference

dog.doggyThing(Dog::barks);

//1. using anonymous inner class

Dog dog = new Dog();

dog.doggyThing(new Doggy() {

@Override

public void doDoggyThing() {

System.out.println("Bang Bang");

}

});

Part of Online video tutorial series: Upgrade to Java 8

Method References – Instance Method

MyCar myCar = new MyCar(500);

//1. using old school anonymous inner class

myCar.getCarServiceStatus(new Servicing() {

@Override

public String getServiceStatus(int distance) {

return new CompanyGarage().repairStatus(distance);

}

});

//2. using lambda expression...

myCar.getCarServiceStatus((distance)->{return new CompanyGarage().repairStatus(distance);});

//3. using Method Reference expression...

myCar.getCarServiceStatus((new CompanyGarage())::repairStatus);

Part of Online video tutorial series: Upgrade to Java 8

Method References – Constructor

class Eggs {

int n;

public Eggs (int n){

this.n = n;

}

}

interface WhiteEggs {

public Eggs getEggs (int noOfEggs);

}

Method References – Constructor

class Eggs {

int n;

public Eggs (int n){

this.n = n;

}

}

interface WhiteEggs {

public Eggs getEggs (int noOfEggs);

}

Method Reference for Constructor:

WhiteEggs whiteEggs = Eggs::new;

Eggs eggs = whiteEggs.getEggs(5);

Part of Online video tutorial series: Upgrade to Java 8

Method References – Constructor

class Eggs {

int n;

public Eggs (int n){

this.n = n;

}

}

interface WhiteEggs {

public Eggs getEggs (int noOfEggs);

}

Lets understand whats happening

Eggs eggs = whiteEggs.getEggs(5);

WhiteEggs whiteEggs = Eggs::new;

Constructor is called on the call of getEggs(5) with 5 as parameter to the constructor

Part of Online video tutorial series: Upgrade to Java 8

Method References – Constructor

class Eggs {

int n;

public Eggs (int n){

this.n = n;

}

}

interface WhiteEggs {

public Eggs getEggs (int noOfEggs);

}

Lets understand whats happening

Eggs eggs = whiteEggs.getEggs(5);

WhiteEggs whiteEggs = Eggs::new;

Object reference is given to instance variable

Part of Online video tutorial series: Upgrade to Java 8

Method References – Constructor

• Observe the convention:

class Eggs {

int n;

public Eggs (int n){

this.n = n;

}

}

interface WhiteEggs {

public Eggs getEggs (int noOfEggs);

}

Eggs eggs = whiteEggs.getEggs(5);WhiteEggs whiteEggs = Eggs::new;

Here the return type of this Functional Interface is Egg which is the type of instance variable egg. We are calling constructor of Egg class by this method reference.

Part of Online video tutorial series: Upgrade to Java 8

Method References – Constructor

• Observe the convention:

class Eggs {

int n;

public Eggs (int n){

this.n = n;

}

}

interface WhiteEggs {

public Eggs getEggs (int noOfEggs);

}

Eggs eggs = whiteEggs.getEggs(5);WhiteEggs whiteEggs = Eggs::new;

Observe that parameter of Functional interface should be same as the constructor of class

Part of Online video tutorial series: Upgrade to Java 8

Found the content Valuable ?Explore the complete course in Online Lecture series:

Upgrade to Java 8

https://www.udemy.com/upgrade-to-java-8/

Salient Features:• Quickly learn concepts such as

• Lambda Expressions• Functional Interfaces• Method References• Streams

• Around 37 Lectures !• Lifetime access• Ask questions online!• Certification of Completion from Udemy!• Also available on iOS and Android