Tuesday, 27 June 2017

Instance method reference of an existing object| Method reference in Java 8 | Java method reference


Click here to watch in Youtube :
https://www.youtube.com/watch?v=CUad5eHBruo&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Instance method reference of an existing object| Method reference in Java 8 | Java method reference 
Mechanic.java
import java.util.function.Consumer;

class Car
{
    private String name;

    public Car(String name)
    {
        super();
        this.name = name;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

}

public class Mechanic
{
    public void fix(Car c)
    {
        System.out.println("Mechanic is fixing " + c.getName());
    }

    public void execute(Car car, Consumer<Car> c)
    {
        c.accept(car);
    }
}
LambdaDemo.java
import java.util.function.Consumer;

public class LambdaDemo
{
    public static void main(String[] args)
    {

        final Mechanic mechanic = new Mechanic();
        Car car = new Car("Honda Jazz");

        // Using an anonymous class
        mechanic.execute(car, new Consumer<Car>()
        {
            public void accept(Car c)
            {
                mechanic.fix(c);
            }
        });

        // Using a lambda expression
        mechanic.execute(car, c -> mechanic.fix(c));

        // Using a method reference
        mechanic.execute(car, mechanic::fix);
    }

}
Output
Mechanic is fixing Honda Jazz
Mechanic is fixing Honda Jazz
Mechanic is fixing Honda Jazz

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/LambdaDemo_Instance_method_ref_mechanic-app.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/LambdaDemo_Instance_method_ref_mechanic-app

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/525432e9afa25ee5052810cb2318f64453ffeffa/BasicJava/LambdaDemo_Instance_method_ref_mechanic-app/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge