Using Method references in java8 -Part 1

             Method reference is a concept in java8. It is simple in format. Generally, it is used when it has objects or primitive data. It is typical lambda expressions.

Method references are categorized into many types listed below.

       1.   Constructor references

       2.   Static references

       3.   Instance references of a particular object

       4.  Instance Method References of an Arbitrary Object of a Particular Type

First two method references are coded with examples.

1.Constructor references:

   A constructor is the first function to be called. Here, the method reference is based on constructor.

A sample java Program to print the message using Constructor Reference is given below.

Steps to follow:

  • ·       First, an interface “MeRefInterface” is created with get method with a String parameter.
  • ·       Create a class MeRefClass and add the definition for constructor with a print function.
  • ·       Now, create a class MeRefConstrutor, use the syntax to implement Constructor reference.
  • o   MeRefInterface myInte = MeRefClass::new;
  • ·       Finally, call the function get to display the output.

#java program to illustrate Constructor Reference:

interface MeRefInterface {

                  MeRefClass get(String s);

              }

              class MeRefClass {

                  MeRefClass(String s) {

                      System.out.println(s);

                  }

              }

              public class MeRefConstructor{

                  public static void main(String[] args) {

                     MeRefInterface myInte = MeRefClass::new;

                      myInte.get("This is sample text by using Constructor Method Reference");

                  }

              }

To compile and execute the file, follow the steps.

C:\raji\blog>javac MeRefConstructor.java

C:\raji\blog>java MeRefConstructor

This is sample text by using Constructor Method Reference

 

2.Static references:

If a method reference is said to be static, it refers a static method. The syntax of the static reference is

Class :: staticMethod

Steps:

  • ·       Create an interface “Message” and declare a method Msg().
  • ·       Now, create a method display() to display the output inside a class “StaticMethodReference”.
  • ·       Inside the main function, write the code for developing object and call the static function display.
  • ·       Call the function Msg by using the object mess.

//Java Program to implement Static References

interface Message {

    void Msg();

}

public class StaticMethodReference {

    public static void display() {

        System.out.println("This is a message from static method.");

    }

    public static void main(String[] args) {

        Message mess = StaticMethodReference::display;

        mess.Msg();

    }

}

Compile and execute the above code. It will give you the output.

C:\raji\blog>javac StaticMethodReference.java

 C:\raji\blog>java StaticMethodReference

This is a message from static method.

These are the ways to implement first two method references. Remaining is in part 2. 

No comments:

Post a Comment