How to create a constructor reference for an array in Java?



In Java, a constructor refers to a special method within a class that is always executed automatically while creating an instance of the class using the new keyword. Its main purpose is initializing the object by providing its fields with initial values so that it is ready for use.

A constructor shares the name of the class itself, has no return type, and can be overloaded so that there will be different ways of initializing the objects. If you don't provide one, Java inserts a default no-argument constructor automatically.

What is a Constructor Reference in Java?

Constructor reference is like method reference but the name of the method is new. We can also create a constructor reference with an array type. For example, if we want to create an integer array using the following constructor reference: int[]:: new, where the parameter is a size of an array.

ArrayTypeName[]::new

Our task is to create a constructor reference for an array in Java. Let's look at some scenarios:

scenario 1

Input: 10
Output: [0] = 0
[1] = 1
[2] = 3
[3] = 8
[4] = 14
[5] = 23
[6] = 33
[7] = 46
[8] = 60
[9] = 77

scenario 2

Input: 5
Output: [0] = 0
[1] = 1
[2] = 3
[3] = 6
[4] = 10

How to Create a Constructor Reference for an Array in Java?

Following is the process to create a constructor reference for an array in Java:

  • Create a Functional Interface: : Declare an interface (for example, ArrayCreator) with a method such as makeArray(int size) that dictates how the array will be produced.
  • Use a Constructor Reference: Pass int[]::new to the functional interface. This expression will create an integer array of the size you provide dynamically.
  • Fill and Display the Array: Use a for-loop to add values to the array and print the values.
ArrayCreator arrayCreator = int[]::new;

Example to Create a Constructor Reference for an Array

Below is an example of implementing an array constructor reference:

@FunctionalInterface
interface ArrayCreator {
   int[] makeArray(int number);
}
public class ArrayConstructorRefTest {
   public static void main(String[] args) {
      ArrayCreator arrayCreator = int[]::new;   // Constructor Reference for an Array
      int[] intArray = arrayCreator.makeArray(10);
      for(int i = 0; i < intArray.length; i++) {
         intArray[i] = i * i - i / 2;
         System.out.println("[" + i + "] = " + intArray[i]);
      }
   }
}

following is the output of the above code :

[0] = 0
[1] = 1
[2] = 3
[3] = 8
[4] = 14
[5] = 23
[6] = 33
[7] = 46
[8] = 60
[9] = 77

Conclusion

In this article, we learned how to initialize an array using constructor references in Java. Rather than typing more, we can use `int[]::new` and create an array of whatever size we need. This reduces code and makes it more readable.

Aishwarya Naglot
Aishwarya Naglot

Writing clean code… when the bugs aren’t looking.

Updated on: 2025-08-20T12:22:04+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements