Java 8 – Constructor Reference | Code Factory


Index Page : Link

Donate : Link

Medium Blog : Link

Applications : Link

package com.codeFactory.methodAndConstructorReference;

class Sample {
	Sample() {
		System.out.println("Sample.Sample()");
	}
}

interface Interf {
	public Sample get();
}

// Using Lambda Expression
public class Test {
	public static void main(String... args) {
		Interf i = () -> {
			return new Sample();
		};
		Sample s = i.get();
	}
}

// Using Constructor Reference
public class Test {
	public static void main(String... args) {
		Interf i = Sample::new;
		Sample s = i.get();
	}
}

Output :

Sample.Sample()

Syntax for Constructor Reference :

  • classname :: new
  • e.g. Sample :: new

Advertisement

4 thoughts on “Java 8 – Constructor Reference | Code Factory”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s