ReviseAlgo Logo

Generics

Generic Methods

Analyze generic method declarations, static generic signatures, and compiler type inference.

Interview: Focuses on type parameter placements, static generic methods, and compile-time type inference.

Last Updated: June 13, 2026 10 min read

A Generic Method is a method that declares its own independent type parameters. The scope of these type parameters is limited to the method itself, allowing both static and instance methods to be parameterized independently of their enclosing class.

Method Scope

Type parameters are placed inside angle brackets before the method's return type: public <T> void print(T val).

Static Safety

Allows static methods to declare their own type parameters, avoiding class-level static constraints.

Type Inference

The compiler infers the type arguments automatically from the arguments passed to the method, simplifying invocations.

Declaring Type Parameters in Signatures

Type parameters must be declared before the return type:

  • Instance Methods: Can combine class-level type parameters with method-level type parameters.
  • Static Methods: Must declare method-level type parameters if they need generic types: public static <T> List<T> emptyList().
  • Explicit Invocation: If type inference fails, specify type arguments explicitly at the call site: ClassName.<String>staticMethod(arg);.

Common Pitfalls

  • Omitting type declarations: Forgetting the type parameter declaration in the method signature, causing the compiler to treat the type parameter as an unknown class name.
  • Overlapping parameter names: Naming a method-level type parameter the same as a class-level parameter, which shadows the class-level type parameter and makes code harder to read.

Best Practices

  • Rely on compiler inference: Let the compiler infer type arguments rather than specifying them explicitly, keeping call sites clean.
  • Limit type parameters: Keep method type parameter counts low to maintain readability.

Interview-Relevant Information

Q1: Where do you declare type parameters for a generic method?
Answer: They are placed inside angle brackets immediately before the method's return type in the signature (e.g. public <T> T execute(T arg)).

Q2: How does type inference simplify generic method calls?
Answer: The compiler infers the required type arguments based on the parameter types passed to the method. This allows invoking generic methods like normal methods: util.print("Hello") instead of util.<String>print("Hello").

Quick Checklist

Can you write a generic static method signature, explain how type inference works, and demonstrate an explicit type argument invocation? If yes, you understand generic methods.

Use Cases

Building static utility collections, transformation pipelines, and converters.

Implementing generic array swapping or sorting helpers.

Common Mistakes

Omitting the type parameter declaration block before the return type.

Shadowing class-level type parameters within instance methods.