Reference to the class constructor
This topic is a continuation of the topic:
Contents
- 1. References to class constructors. General form
- 2. Examples of using class constructors references
- Related topics
Search other websites:
1. References to class constructors. General form
The lambda expression mechanism can be used for class constructors. Constructor references are created in the same way as method references.
The general form of the syntax for declaring a constructor reference is as follows
class_name::new
A constructor reference can be assigned to a functional interface reference, provided the constructor signature is compatible with the functional interface method signature.
⇑
2. Examples of using class constructors references
2.1. Reference to the class constructor. The class implements a triangle along its sides
The example demonstrates the use of a reference to the Triangle class constructor, which implements a triangle along its sides.
// A class that implements a triangle along its sides class Triangle { private double a, b, c; // Constructor public Triangle(double a, double b, double c) { this.a = a; this.b = b; this.c = c; } // Access methods public double GetA() { return a; } public double GetB() { return b; } public double GetC() { return c; } // Method for printing field values public void Print(String text) { System.out.println(text); System.out.println("a = " + a + ", b = " + b + ", c = " + c); } } // Functional interface for the triangle class interface IFunction { // The signature is the same as the constructor signature of the Triangle class Triangle Function(double a, double b, double c); } public class DemoRefMethod { public static void main(String[] args) { // 1. Declare a reference to the functional interface IFunction ref; // 2. Assign constructor value to reference. ref = Triangle::new; // 3. Create the instance of Triangle class Triangle tr = ref.Function(5, 4, 5); tr.Print("tr:"); } }
In the above code in the string
ref = Triangle::new;
a ref to a functional interface is assigned the value of the Triangle class constructor. Having a reference to the constructor, you can create an instance of the Triangle class by calling the Function() method of the IFunction interface as shown below
Triangle tr = ref.Function(5, 4, 5);
The result of the program
tr: a = 5.0, b = 4.0, c = 5.0
⇑
2.2. Reference to the constructor of a generic (template) class
Task. Implement the template class Array<T> containing methods for operating on an array of data:
- calculating the sum of array elements;
- calculating the maximum element in the array.
Demonstrate using a class constructor reference to create the instance of the Array<T> class.
Solution.
// A template class that contains methods for operating on an array of data of type T class Array<T extends Number> { // Class constructor without parameters Array() { } // Method for calculating the sum of array elements Double Sum(T[] A) { Double sum = 0.0; for (int i=0; i<A.length; i++) sum += A[i].doubleValue(); return sum; } // Method for calculating the maximum element in the array Double Max(T[] A) { Double max = A[0].doubleValue(); for (int i=1; i<A.length; i++) if (max<A[i].doubleValue()) max = A[i].doubleValue(); return max; } } // 2. Generalized functional interface, // designed to work with Array class interface IFunction<T extends Number> { // The signature of this method must match // the signature of the constructor of the class Array<T> Array<T> Function(); } public class DemoRefMethod { public static void main(String[] args) { // Demonstration of using a reference to the constructor of the Array<T> class // 1. For Integer type // 1.1. Declare a reference to functional interface IFunction<T> IFunction<Integer> refInt; // 1.2. Assign the value of reference to constructor. refInt = Array::new; // 1.3. Create an instance of Array<T> class using constructor // with reference to the Integer type Array<Integer> objInt = refInt.Function(); // 1.4. The array under test Integer[] AI = { 1, 3, 7 }; // 1.5. Calculate the sum of an array AI Double sum = objInt.Sum(AI); System.out.println("sum = " + sum); // sum = 11.0 // 2. Binding to the type Float // 2.1. Create an instance of the Array<T> class with binding to the Float type IFunction<Float> refFloat = Array::new; // 2.2. Create the instance of Array<T> for Float type Array<Float> objFloat = refFloat.Function(); // 2.3. The array under test Float[] AF = { 1.8f, -2.5f, 3.4f, 2.9f, 1.7f }; // 2.4. Calculate the maximum value Double max = objFloat.Max(AF); System.out.println("max = " + max); // max = 3.4000000953674316 } }
The above code uses the call to access the constructor of the Array<T> class
Array::new
The Array<T> class does not need to declare a parameterless constructor Array(). This is explained by the following rule: if no constructor is declared in a class, then the compiler creates an implicit parameterless constructor called the default constructor.
⇑
Related topics
- Types of method references. Reference to static method. Reference to instance method
- Constructors. Parameterized constructors. The keyword this. Garbage collection. The finalize() method. Examples
⇑