Method and Constructor References

by Santosh K Saha | Category Java-8


Java 8 provides a new feature called method reference. Method references are a way to make the lambda expression shorter when it is used to just refer or invoke a method.


Types of Method References

  • Reference to static method
  • Reference to instance method from instance ( Bound Instance Method Reference )
  • Reference to instance method from class type ( Unbound Instance Method Reference )
  • Reference to constructor

Lets understand the method reference with examples


Reference to static method :

Used to refer static methods in a class

Syntax :

ClassName :: staticMethodName

@FunctionalInterface
public interface FunctionalInterfaceOne {

	public void abc(); // ()->void

}

@FunctionalInterface
public interface FunctionalInterfaceTwo {

	public void xyz(String name); // (String)->void

}

@FunctionalInterface
public interface FunctionalInterfaceThree {

	public double mno(long i, long j); // (long, long)->double

}

public class MethodReference {

	public static void staticMethodOne() {
		System.out.println("staticMethodOne---->>");
	}

	public static void staticMethodTwo(String name) {
		System.out.println("staticMethodTwo---->>" + name);
	}

	public static double staticMethodThree(long i, long j) {
		System.out.println("staticMethodThree---->>" + i + "---" + j);
		return i + j;
	}

}

public class App {

	public static void main(String[] args) {

		// ----------- Using Anonymous Class -----------------------
		FunctionalInterfaceOne a1 = new FunctionalInterfaceOne() {

			@Override
			public void abc() {
				MethodReference.staticMethodOne();
			}
		};

		FunctionalInterfaceTwo a2 = new FunctionalInterfaceTwo() {

			@Override
			public void xyz(String name) {
				MethodReference.staticMethodTwo(name);
			}
		};

		FunctionalInterfaceThree a3 = new FunctionalInterfaceThree() {

			@Override
			public double mno(long i, long j) {
				return MethodReference.staticMethodThree(i, j);
			}
		};

		// ----------- Using Lambda Expression -----------------------

		FunctionalInterfaceOne l1 = () -> MethodReference.staticMethodOne();
		FunctionalInterfaceTwo l2 = (name) -> MethodReference.staticMethodTwo(name);
		FunctionalInterfaceThree l3 = (i, j) -> MethodReference.staticMethodThree(i, j);

		// --------------Using Method Reference --------------------------------

		FunctionalInterfaceOne m1 = MethodReference::staticMethodOne;
		FunctionalInterfaceTwo m2 = MethodReference::staticMethodTwo;
		FunctionalInterfaceThree m3 = MethodReference::staticMethodThree;

		System.out.println("============Call method of anonymous class================");
		a1.abc();
		a2.xyz("Ram");
		a3.mno(5, 6);

		System.out.println("============Call method using lambda expression================");
		l1.abc();
		l2.xyz("Hari");
		l3.mno(6, 7);

		System.out.println("============Call method using method reference================");
		m1.abc();
		m2.xyz("Shyam");
		m3.mno(7, 8);

	}

}


Reference to instance method from instance ( Bound Instance Method Reference ) :

Used to refer instance method in a class

Syntax :

ObjectReference::instanceMethodName

@FunctionalInterface
public interface FunctionalInterfaceOne {

	public void abc(); // ()->void

}

@FunctionalInterface
public interface FunctionalInterfaceTwo {

	public void xyz(String name); // (String)->void

}

@FunctionalInterface
public interface FunctionalInterfaceThree {

	public double mno(long i, long j); // (long, long)->double

}

public class MethodReference {

	public void instanceMethodOne() {
		System.out.println("instanceMethodOne---->>");
	}

	public void instanceMethodTwo(String name) {
		System.out.println("instanceMethodTwo---->>" + name);
	}

	public double instanceMethodThree(long i, long j) {
		System.out.println("instanceMethodThree---->>" + i + "---" + j);
		return i + j;
	}

}

public class App {

	public static void main(String[] args) {

		MethodReference mr = new MethodReference();

		// ----------- Using Anonymous Class -----------------------
		FunctionalInterfaceOne a1 = new FunctionalInterfaceOne() {

			@Override
			public void abc() {
				mr.instanceMethodOne();
			}
		};

		FunctionalInterfaceTwo a2 = new FunctionalInterfaceTwo() {

			@Override
			public void xyz(String name) {
				mr.instanceMethodTwo(name);
			}
		};

		FunctionalInterfaceThree a3 = new FunctionalInterfaceThree() {

			@Override
			public double mno(long i, long j) {
				return mr.instanceMethodThree(i, j);
			}
		};

		// ----------- Using Lambda Expression -----------------------

		FunctionalInterfaceOne l1 = () -> mr.instanceMethodOne();
		FunctionalInterfaceTwo l2 = (name) -> mr.instanceMethodTwo(name);
		FunctionalInterfaceThree l3 = (i, j) -> mr.instanceMethodThree(i, j);

		// --------------Using Method Reference --------------------------------

		FunctionalInterfaceOne m1 = mr::instanceMethodOne;
		FunctionalInterfaceTwo m2 = mr::instanceMethodTwo;
		FunctionalInterfaceThree m3 = mr::instanceMethodThree;

		System.out.println("============Call method of anonymous class================");
		a1.abc();
		a2.xyz("Ram");
		a3.mno(5, 6);

		System.out.println("============Call method using lambda expression================");
		l1.abc();
		l2.xyz("Hari");
		l3.mno(6, 7);

		System.out.println("============Call method using method reference================");
		m1.abc();
		m2.xyz("Shyam");
		m3.mno(7, 8);

	}

}


Reference to instance method from class type ( Unbound Instance Method Reference ) :

Used to refer instance method in a class at run time

Syntax :

ClassName::instanceMethodName

@FunctionalInterface
public interface FunctionalInterfaceOne {

	public void abc(MethodReference mr); // ()->void

}

@FunctionalInterface
public interface FunctionalInterfaceTwo {

	public void xyz(MethodReference mr, String name); // (String)->void

}

@FunctionalInterface
public interface FunctionalInterfaceThree {

	public double mno(long i, long j); // (long, long)->double

}

public class MethodReference {

	public void instanceMethodOne() {
		System.out.println("instanceMethodOne---->>");
	}

	public void instanceMethodTwo(String name) {
		System.out.println("instanceMethodTwo---->>" + name);
	}

	public double instanceMethodThree(long i, long j) {
		System.out.println("instanceMethodThree---->>" + i + "---" + j);
		return i + j;
	}

}

public class App {

	public static void main(String[] args) {

		// ----------- Using Anonymous Class -----------------------
		FunctionalInterfaceOne a1 = new FunctionalInterfaceOne() {

			@Override
			public void abc(MethodReference mr) {
				mr.instanceMethodOne();
			}
		};

		FunctionalInterfaceTwo a2 = new FunctionalInterfaceTwo() {

			@Override
			public void xyz(MethodReference mr, String name) {
				mr.instanceMethodTwo(name);
			}
		};

		FunctionalInterfaceThree a3 = new FunctionalInterfaceThree() {

			@Override
			public double mno(long i, long j) {
				return new MethodReference().instanceMethodThree(i, j);
			}
		};

		// ----------- Using Lambda Expression -----------------------

		FunctionalInterfaceOne l1 = (mr) -> new MethodReference().instanceMethodOne();
		FunctionalInterfaceTwo l2 = (mr, name) -> mr.instanceMethodTwo(name);
		FunctionalInterfaceThree l3 = (i, j) -> new MethodReference().instanceMethodThree(i, j);

		// --------------Using Method Reference --------------------------------

		FunctionalInterfaceOne m1 = MethodReference::instanceMethodOne;
		FunctionalInterfaceTwo m2 = MethodReference::instanceMethodTwo;
		FunctionalInterfaceThree m3 = new MethodReference()::instanceMethodThree;

		MethodReference mr = new MethodReference();

		System.out.println("============Call method of anonymous class================");
		a1.abc(mr);
		a2.xyz(mr, "Ram");
		a3.mno(5, 6);

		System.out.println("============Call method using lambda expression================");
		l1.abc(mr);
		l2.xyz(mr, "Hari");
		l3.mno(6, 7);

		System.out.println("============Call method using method reference================");
		m1.abc(mr);
		m2.xyz(mr, "Shyam");
		m3.mno(7, 8);

	}

}


Reference to constructor :

Used to refer constructor

Syntax :

ClassName :: new

@FunctionalInterface
public interface FunctionalInterfaceOne {

	public MethodReference abc(); // ()->void

}

@FunctionalInterface
public interface FunctionalInterfaceTwo {

	public MethodReference xyz(String name); // (String)->void

}

@FunctionalInterface
public interface FunctionalInterfaceThree {

	public MethodReference mno(long i, long j); // (long, long)->double

}

public class MethodReference {

	private String name;
	private long i, j;

	public MethodReference() {

	}

	public MethodReference(String name) {
		this.name = name;
	}

	public MethodReference(long i, long j) {
		this.i = i;
		this.j = j;
	}

	// Getter Setter

}

public class App {

	public static void main(String[] args) {

		// ----------- Using Anonymous Class -----------------------
		FunctionalInterfaceOne a1 = new FunctionalInterfaceOne() {

			@Override
			public MethodReference abc() {
				return new MethodReference();
			}
		};

		FunctionalInterfaceTwo a2 = new FunctionalInterfaceTwo() {

			@Override
			public MethodReference xyz(String name) {
				return new MethodReference(name);
			}
		};

		FunctionalInterfaceThree a3 = new FunctionalInterfaceThree() {

			@Override
			public MethodReference mno(long i, long j) {
				return new MethodReference(i, j);
			}
		};

		// ----------- Using Lambda Expression -----------------------

		FunctionalInterfaceOne l1 = () -> new MethodReference();
		FunctionalInterfaceTwo l2 = (name) -> new MethodReference(name);
		FunctionalInterfaceThree l3 = (i, j) -> new MethodReference(i, j);

		// --------------Using Method Reference --------------------------------

		FunctionalInterfaceOne m1 = MethodReference::new;
		FunctionalInterfaceTwo m2 = MethodReference::new;
		FunctionalInterfaceThree m3 = MethodReference::new;

		MethodReference mr = new MethodReference();

		System.out.println("============Call method of anonymous class================");
		a1.abc();
		a2.xyz("Ram");
		a3.mno(5, 6);

		System.out.println("============Call method using lambda expression================");
		l1.abc();
		l2.xyz("Hari");
		l3.mno(6, 7);

		System.out.println("============Call method using method reference================");
		m1.abc();
		m2.xyz("Shyam");
		m3.mno(7, 8);

	}

}
Note: Like our facebook page to get updated of free classes and tutorials.