Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How (this::methodName) is working in this code?

Tags:

android

In this question there is one line,

findViewById(R.id.go_to_play_store).setOnClickListener(this::goToPlayStore);

how does this line is exactly handling the click listener ?

like image 762
Darshan Soni Avatar asked Dec 23 '22 21:12

Darshan Soni


2 Answers

Java8 introduces concept of Method references and Functional interfaces. If function onClickListener requires a function with one argument(a.k.a Functional Interface) then if return types and argument types match your function(which is goToPlayStore) then you can pass its reference as functional interface.

like image 155
Murat Mustafin Avatar answered Jan 06 '23 05:01

Murat Mustafin


This is the new JAVA 8 language feature Lambda Expressions.

:: refer to a new syntax in Java 8 known as method references. You can reference a class or instance and pass along the method that will handle the event

On click its calling function called goToPlayStore () located in that activity or fragment.

like image 30
taman neupane Avatar answered Jan 06 '23 03:01

taman neupane