0
0
JavaHow-ToBeginner · 3 min read

How to Create Generic Class in Java: Syntax and Example

In Java, you create a generic class by adding a type parameter in angle brackets after the class name, like class Box<T>. This allows the class to work with any data type specified when creating an object, improving code reusability and type safety.
📐

Syntax

A generic class in Java uses a type parameter inside angle brackets <> after the class name. This type parameter acts as a placeholder for the actual data type you want to use when creating an object.

  • T: A common name for the type parameter, but you can use any valid identifier.
  • class Box<T>: Declares a generic class named Box with type parameter T.
  • T value;: A field of type T inside the class.
  • Methods: Can use T as a type for parameters and return values.
java
public class Box<T> {
    private T value;

    public Box(T value) {
        this.value = value;
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }
}
💻

Example

This example shows how to create and use a generic class Box<T> with different data types like Integer and String. It demonstrates type safety and reusability without casting.

java
public class Box<T> {
    private T value;

    public Box(T value) {
        this.value = value;
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }

    public static void main(String[] args) {
        Box<Integer> intBox = new Box<>(123);
        System.out.println("Integer Value: " + intBox.getValue());

        Box<String> strBox = new Box<>("Hello Generics");
        System.out.println("String Value: " + strBox.getValue());
    }
}
Output
Integer Value: 123 String Value: Hello Generics
⚠️

Common Pitfalls

Common mistakes when creating generic classes include:

  • Using raw types (no type parameter), which loses type safety and causes warnings.
  • Trying to create instances of the generic type parameter directly (e.g., new T()), which is not allowed.
  • Using primitive types like int instead of wrapper classes like Integer as type arguments.

Always specify the type parameter when creating objects and use wrapper classes for primitives.

java
/* Wrong: Using raw type - no type safety and warning */
Box rawBox = new Box("Raw type");
String val = (String) rawBox.getValue(); // Requires casting

/* Right: Using generic type for safety */
Box<String> strBox = new Box<>("Safe generic");
String safeVal = strBox.getValue(); // No casting needed
📊

Quick Reference

  • Declare generic class: class ClassName<T> { ... }
  • Use type parameter T for fields, methods, and constructors.
  • Create objects with specific types: ClassName<Integer> obj = new ClassName<>(value);
  • Cannot instantiate T directly inside the class.
  • Use wrapper classes for primitives (e.g., Integer instead of int).

Key Takeaways

Use angle brackets <> after the class name to declare a generic class with a type parameter.
Generic classes improve code reuse and type safety by allowing any data type to be specified when creating objects.
Avoid raw types to prevent unsafe casts and compiler warnings.
You cannot create instances of the generic type parameter directly inside the class.
Use wrapper classes for primitive types when specifying type arguments.