> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Java Constructor Reference

A constructor reference is a specialized form of a method reference in Java that provides a declarative shorthand for instantiating objects or arrays. It functions as a compact lambda expression, allowing a constructor to be treated as a first-class function and assigned to a compatible functional interface.

## Syntax

The syntax utilizes the double-colon (`::`) operator appended to a class or array type, followed by the `new` keyword.

```java theme={"dark"}
// Object instantiation
ClassName::new

// Array instantiation
Type[]::new
```

## Resolution and Target Typing

Constructor references do not specify arguments directly. Instead, the Java compiler resolves the appropriate constructor through **target typing**. The compiler examines the signature of the single abstract method (SAM) within the target functional interface and matches its parameter types to an available constructor in the specified class.

### 1. No-Argument Constructor

If the functional interface method takes no arguments and returns an object, the compiler binds to the default or no-argument constructor.

```java theme={"dark"}
// Lambda equivalent: () -> new StringBuilder()
Supplier<StringBuilder> supplier = StringBuilder::new; 
```

### 2. Parameterized Constructor

If the functional interface method accepts arguments, the compiler resolves to the overloaded constructor matching those exact parameter types.

```java theme={"dark"}
// Lambda equivalent: (str) -> new String(str)
Function<String, String> function = String::new; 

// Lambda equivalent: (capacity, loadFactor) -> new HashMap<>(capacity, loadFactor)
BiFunction<Integer, Float, HashMap<String, String>> biFunction = HashMap::new;
```

### 3. Array Constructor

Array constructor references map to a functional interface whose method accepts a single `int` parameter (representing the array length) and returns an array of the specified type.

```java theme={"dark"}
// Lambda equivalent: (length) -> new int[length]
IntFunction<int[]> arrayCreator = int[]::new;
```

## Generic Type Inference

When dealing with generic classes, the compiler infers the type arguments from the context of the functional interface. However, explicit type witnesses can be provided between the class name and the `::` operator if inference is insufficient.

```java theme={"dark"}
// Inferred type arguments
Supplier<List<String>> inferred = ArrayList::new;

// Explicit type witness
Supplier<List<String>> explicit = ArrayList<String>::new;
```

## Compilation Mechanics

At compile time, a constructor reference is translated into an `invokedynamic` instruction. The JVM dynamically generates the implementation of the functional interface, routing the SAM invocation directly to the `invokespecial` bytecode instruction associated with the resolved constructor, avoiding the creation of an anonymous inner class.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Java Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
