Ramesh Pasupuleti Ramesh Pasupuleti -4 years ago 54
Android Question

How to refactor my code to support new features of java 8 such as 'method references' for instance methods?

I have been using Android Studio 3.0 Preview 1 to refactor my code.

I have enable Java-8 features for my code by following this link :

https://developer.android.com/studio/preview/features/java8-support.html

I have refactored my code with lambdas by following 'Android Studio' suggestion.

Now, I am trying to use method reference where every latest Android studio suggests.

How to call instance method that defined in the same class using method reference concept of java 8?

sample code :

Handler handler = getHandler();
handler.dowork(() -> refreshIt());// Need convert this line using method references and refreshIt() is an instance method.


I am doing manually. Is there any way that Android studio(on Mac) refactor those method reference suggestions automatically?

Thanks in advance.

Answer Source

You can simply use this:

handler.dowork(this::refreshIt);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download