Generic Constructors in Java

Naveen AnamaneniUpdated · 3 min read
mediumJava 25javagenerics

Just as a method can have its own type parameter, so can a constructor. A generic constructor accepts an argument of any type at the moment the object is built, uses it, and then doesn't keep that type around: the class itself stays non-generic. It's a rare feature, but it explains how type parameters attach to exactly the code that needs them.

A Constructor That Accepts Any Type

Labels.java▶ Try it
public class Labels {
    static class Label {
        private final String text;
 
        <T> Label(T value) {
            text = value + " (" + value.getClass().getSimpleName() + ")";
        }
 
        String text() { return text; }
    }
 
    public static void main(String[] args) {
        System.out.println(new Label(42).text());
        System.out.println(new Label("Srihitha").text());
        System.out.println(new Label(3.5).text());
    }
}
Output
42 (Integer)
Srihitha (String)
3.5 (Double)

How it works

  1. <T> Label(T value) declares a type parameter on the constructor, before its name.
  2. Each new Label(...) call infers T from its argument: Integer, String, Double.
  3. The constructor turns the value into text and stores only a String. The class Label is not generic: you write Label, never Label<Integer>.

Generic Constructor vs Generic Class

Generic class Box<T>Generic constructor <T> Label(T v)
The type is part of the objectYes: Box<String> vs Box<Integer>No: every object is just a Label
Fields can use the typeYesNo; the type is gone after construction
Typical useContainers, pairs, resultsAccepting flexible input that's converted at once

A class can be generic and have a generic constructor with a different parameter, but that's unusual and hard to read.

Common Mistakes

Writing new Label<Integer>(42). Label isn't generic; the constructor's type is inferred.

Trying to keep the value in a field of type T. T exists only inside the constructor.

Confusing it with the diamond. new Box<>(...) infers a generic class's type; a generic constructor infers its own.

Practice

  1. Easy: Write a class Printable with a generic constructor that stores "[" + value + "]" as a string, and create it with three different types.
  2. Harder: Rewrite Label to use a static generic factory method Label.of(...) instead, and compare how the calls read.

Quick check: Generic constructors

4 questions
1Where is a generic constructor's type parameter written?
2Is a class with a generic constructor itself generic?
3Can a field use the constructor's T?
4What is a common, clearer alternative?

Key Takeaways

Was this tutorial helpful?

Lesson 242 of 482