Java 8 — Method Reference | Code Factory

Method Reference by Double Colon (::) Operator :

package com.codeFactory.methodAndConstructorReference;interface Interf {
public void m1();
}
public class Test {
public static void main(String... args) {
Interf i = () -> System.out.println("Lambda Expression");
i.m1();
}
}
Lambda Expression
package com.codeFactory.methodAndConstructorReference;interface Interf {
public void m1();
}
public class Test {
public static void main(String... args) {
Interf i = Test::m2;
i.m1();
}

public static void m2() {
System.out.println("Method Reference");
}
}
Method Reference
  • Advantage of Method Reference is code reusability.
  • Referer and Refering method both should have same argument type, return type can be different, modifier can be different and that method can be static or non-static.
  • Method reference is alternative syntax to Lambda Expression.
  • Function Interface can refer Lambda Expression, Functional Interface can refer method reference.
- Static method :
- classname :: method name
- e.g. Test::m2
- Instance method :
- object ref :: method name
- e.g. Test t = new Test(); ------> t::m2;
1. By extend thread class
2. By implementing runnable (3 ways)
2.1 Runnable r = new MyRunnable()
2.2 Runnable r = Lambda Expression
2.3 Runnable r = Method Reference
package com.codeFactory.methodAndConstructorReference;public class Test {
public static void main(String... args) {
Runnable r = () -> {
for(int i=0; i<5; i++) {
System.out.println("Child Thread");
}
};

Thread t = new Thread(r);
t.start();
for(int i=0; i<5; i++) {
System.out.println("Main Thread");
}
}
}
Main Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Main Thread
Main Thread
Main Thread
Main Thread
package com.codeFactory.methodAndConstructorReference;public class Test {
public void m1() {
for(int i=0; i<5; i++) {
System.out.println("Child Thread");
}
}

public static void main(String... args) {
Test t = new Test();
Runnable r = t :: m1;
Thread t1 = new Thread(r);
//Thread t1 = new Thread(new Test()::m1);
t1.start();
for(int i=0; i<5; i++) {
System.out.println("Main Thread");
}
}
}
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Sketchnote: Game Architecture

Microservices and Distributed Systems

Introducing the Notion API Ruby Gem

Displaying images in Android app: maintainable, testable, painless. Part II

gRPC Request Routing (Header Based) AWS ALB

Deploying an Express API to Google App Engine

A Snowflake development environment — Part 2 — SQL & Git

XP.Network provides cross-chain NFT-bridge for Smartplace

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Singleton Design Pattern in Java

Naming Conventions in Java

Java 14 Features