Studying Java 8 (see constructor)

Now it's a constructor reference. The method reference in the previous article sensuously specified that the implementation of the functional IF abstract method should be a specific method. Is it an image that you can specify the method and the constructor as specific?

As with the last time, prepare some functional IFs as appropriate. SampleBean is a dedicated return value bean prepared for methodA of SampleFunctionalIF3, and SampleBeanExt is a bean that just extends it and creates a default constructor. getter and setter are omitted.

@FunctionalInterface
public interface SampleFunctionalIF<T> {
	String methodA(T object);
}
@FunctionalInterface
public interface SampleFunctionalIF2 {
	String methodA();
}

@FunctionalInterface
public interface SampleFunctionalIF3 {
	SampleBean methodA(String A, String B);
}
public class SampleBean {

	private String val1;
	private String val2;
}
public class SampleBeanExt extends SampleBean {

	SampleBeanExt(String param1, String param2) {
		this.setVal1(param1);
		this.setVal2(param2);
		System.out.println("SampleBeanExt constructor called");
	}
}

So, the following is trying to execute these.

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

		//With argument (String)in the case of
		//New String at the timing when the abstract method methodA specified by the functional IF is called("xyz")Is called
		System.out.println("-----Example 1-----");
		SampleFunctionalIF<String> sample = String::new;
		System.out.println(sample.methodA("xyz"));

		//No arguments (String)in the case of
		//New String at the timing when the abstract method methodA specified by the functional IF is called()Is called
		//The size is 0 because it is an empty string
		System.out.println("-----Example 2-----");
		SampleFunctionalIF2 sample2 = String::new;
		String s2 = sample2.methodA();
		System.out.println(s2.length());

		//With 2 arguments
		//At the timing when the abstract method methodA specified by the functional IF is called
		// new SampleBeanExt("hello","world")Is called
		System.out.println("-----Example 3-----");
		SampleFunctionalIF3 sample3 = SampleBeanExt::new;
		SampleBean bean = sample3.methodA("hello", "world");
		System.out.println("1:" + bean.getVal1());
		System.out.println("2:" + bean.getVal2());
	}
}

And the execution result

-----Example 1-----
xyz
-----Example 2-----
0
-----Example 3-----
SampleBeanExt constructor called
1:hello
2:world

It seems like it's just after doing a method reference. However, I can't think of any use for this.

Recommended Posts

Studying Java 8 (see constructor)
Studying Java 8 (see method)
Studying Java ―― 3
Studying Java ―― 9
java (constructor)
Studying Java ―― 4
Studying Java -5
Studying Java # 0
Studying Java ―― 8
Java constructor
Studying Java ②
Studying Java ―― 7
Studying Java ―― 2
Studying Java ①
Studying Java -10
Studying Java 8 (Optional)
Studying java9 (jShell)
Studying Java 8 (Stream)
Understand java constructor
Studying how to use the constructor (java)
Studying Java 8 (Collector / Collectors)
Studying Java ~ Part 8 ~ Cast
JAVA constructor call processing
Studying Java 8 (lambda expression)
I studied the constructor (java)
[For beginners] Summary of java constructor
Studying Java # 6 (How to write blocks)
Java
Java overload constructor starting from beginner
Studying Java 8 (StaticIF and Default methods)
Studying Java 8 (String Joiner and join)
Java
Studying java9 (dropWhile, takeWhile and Stream.ofNullable)
constructor
Studying Java 8 (date API in java.time package)
[# 1 Java] Basics of Java-Major premise before studying-
Studying java (Basic Information Technology Engineer Examination)
Force non-instantiation with Effective Java private constructor
[Java] Instance method, instance field, class method, class field, constructor summary