instagram

Constructor reference in java 8

Constructor reference in java 8

This article is about constructor reference in java 8. Like a method reference we can also use constructor reference in java 8. Read more about method reference here.

Now before start with example code you need to take care of few things. Constructor reference only works with Functional Interface. Read more about Functional Interface. It means like lambda expression, constructor reference is use to provide implementation of interface.

Like method reference, :: is use for passing a constructor reference.

<LEFT SIDE - CLASS NAME > :: <RIGHT - new KEYWORD >

Left side is always your class name and right side new keyword.

Example Code :

Now lets start with simple Functional Interface Car.

@FunctionalInterface
public interface Car {

	public BMWCar getCar(String id);

}

Here BMWCar is a class.

public class BMWCar {

	private String carId = "";

	public BMWCar(String carId) {
		this.carId = carId;
		System.out.println("Calling with carId :" + carId);
	}

	public BMWCar() {
		System.out.println("Calling without carId");
	}

	public void printCarModel() {
		
		System.out.println("Car Model : 2014 CarId :" + carId);

	}

}

Now first we will create implementation of Car interface with normal Java 7 way.

Simple Java 7 way

public class Main {

	public static void main(String[] args) {

    	Car car = new Car() {

			@Override
			public BMWCar getCar(String id) {
				return new BMWCar(id);
			}

		};

		car.getCar("3783").printCarModel();

	}

}

Output :

Calling with carId :3783
Car Model : 2014 CarId :3783

Using Lambda Expression

public class Main {

	public static void main(String[] args) {

		Car car = (id) -> new BMWCar(id);
		car.getCar("3783").printCarModel();

	}
}

Output :

Calling with carId :3783
Car Model : 2014 CarId :3783

Using Method Reference

public class Main {

	public static void main(String[] args) {

		Car car = Main::myCarWithId;
		car.getCar("3783").printCarModel();

	}

	public static BMWCar myCarWithId(String id) {
		System.out.println("Calling method myCarWithId");
		return new BMWCar(id);
	}

}

Output :

Calling with carId :3783
Car Model : 2014 CarId :3783

Using Constructor Reference

Before going further did you notice what we are trying to achieve in above code. Just look at Car interface’s abstract method public BMWCar getCar(String id);, In short we are trying to provide implementation of Car interface and it’s method public BMWCar getCar(String id);
This getCar returns BMWCar.

Now what does new keyword do in java ????????????

For example we want to create an object of BMWCar class. Below is a simple way to do this, using new keyword.

BMWCar bmw = new BMWCar("3783");

Again the question is what does new keyword do in above code ????????????
Answer is new keyword creates a brand new object of BMWCar and return it.

Now look at above code again. In Method Reference example there is an one static method public static BMWCar myCarWithId(String id); it also returns BMWCar object like new keyword.

Now we are going to replace this Method Reference with Constructor Reference.

public class Main {

	public static void main(String[] args) {

		// The target type of this expression must be a functional interface
		// means here Car interface is must be a functional interface.
		Car car = BMWCar::new;

		car.getCar("3783").printCarModel();

	}

}

Output :

Calling with carId :3783
Car Model : 2014 CarId :3783

Now BMWCar::new; This simple code does three main things given below.

1. Returning implementation of public BMWCar getCar(String id); method. So compiler will use this method’s implementation and creates implementation of Car interface.
2. It returns BMWCar object like public BMWCar getCar(String id); method. Bcz you can see new keyword, As I told you new keyword creates object.
3. It used public BMWCar(String carId) constructor instead of public BMWCar() for creating object. But why ???? Bcz Car interface’s abstract method public BMWCar getCar(String id); wants String argument.

BMWCar::new; compiler will compile this code and convert it in below code :

// Here BMWCar::new; It means below code : 
     new Car() {

			@Override
			public BMWCar getCar(String id) {
				return new BMWCar(id);
			}

		};

That’s it, I hope you enjoyed it.

Share