Java – Method References

Method references:

  • It is JDK8 feature.
  • It is used to refer any method of functional interface easily.
  • Any method definition can be assigned to functional interface method identity and access the using its identity.

Method reference to static method as follows:

@FunctionalInterface
interface Test
{
            void abc();
}
class Demo
{
            static void fun() {
                        System.out.println(“fun…”);
            }
}
class Main
{
            public static void main(String[] args) {
                        Test obj = Demo::fun;
                        obj.abc();
            }
}

Method reference to an instance method:

@FunctionalInterface
interface Test
{
            void abc();
}
class Demo
{
            void fun() {
                        System.out.println(“fun…”);
            }
}
class Main
{
            public static void main(String[] args) {
                        Test obj = new Demo()::fun;
                        obj.abc();
            }
}
Scroll to Top