Java Method Reference ::

Java Method Reference ::

Method reference in Java 8 is the ability to use a method as an argument for a matching functional interface. 

:: (double colon) is the operator used for method reference in Java. An interface with only one method is called a functional interface. 

For example, Comparable, Runnable, AutoCloseable are some functional interfaces in Java.

Its there all through the Internet saying Java does not have the scope resolution operator (::), in comparison with C++. 

Its on the way and it will be here with Java 8. Between Java and C++, only the operator :: (double colon) is same, 

the way it is implemented is different. So we cannot call it as scope resolution operator in Java. 

It is used for method referencing and not for scope resolution.

Method-Reference

Method References and Lambda Expressions

Method reference using :: is a convenience operator. Method reference is one of the features belonging to Java lambda expressions

Method reference can be expressed using the usual lambda expression syntax format using –> 

In order to make it more simple :: operator can be used.

In the last tutorial, I gave a list of examples for Java lambda expressions

Which will give detail on different ways of using lambda expressions and I recommend you to go through it using JDK 8. 

In that article refer example 5 (Lambda Expression Sorting), there we can see method reference used.

Syntax: <classname or instancename>::<methodname>


The method in the functional interface and the passing method reference should match for the argument and returntype. 

Method reference can be done for both static and class methods. Let us consider the following example to understand this. 

In AutoCloseable interface we have the method of signature as,

void close throws Exception()

In our example below we have the method with signature,

void close()

So these two methods are matching in terms of argument and returntype (type match). 

Therefore we can use the method we have written for the functional interface AutoCloseable as a method reference.

Method Reference Example

package com.javapapers.java8;

public class MethodReferenceExample {
	void close() {
		System.out.println("Close.");
	}

	public static void main(String[] args) throws Exception {
		MethodReferenceExample referenceObj = new MethodReferenceExample();
		try (AutoCloseable ac = referenceObj::close) {
		}
	}
}

Constructor References

Constructor reference is similar to method references, wherein used at the place of constructors. 

It gives best opportunity to use at factory scenarios. in the following example, we are using the zoo constructor with argument as constructor reference. 

I have provided the equivalent line in lambda expression in the code, just to understand it.

Constructor Reference Example

package com.javapapers.java8;

import java.util.List;
import java.util.ArrayList;

class Zoo {
    private List animalList;
    public Zoo(List animalList) {
        this.animalList = animalList;
		System.out.println("Zoo created.");
    }
}

interface ZooFactory {
    Zoo getZoo(List animals);
}
 
public class ConstructorReferenceExample {

    public static void main(String[] args) {
		//following commented line is lambda expression equivalent
		//ZooFactory zooFactory = (List animalList)-> {return new Zoo(animalList);};	
		ZooFactory zooFactory = Zoo::new;
		System.out.println("Ok");		
		Zoo zoo = zooFactory.getZoo(new ArrayList());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值