Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Write a generic method that returns the first element of any list or a default value, and a generic class Pair<A, B>. Explain where the type parameter is declared in each case.

The method is static <T> T firstOr(List<? extends T> list, T fallback) { return list.isEmpty() ? fallback : list.get(0); } and the class is record Pair<A, B>(A first, B second) {}. A generic method declares its type parameters in angle brackets before the return type, and they exist only inside that method. A generic class or record declares them after its name, and they are available to all its instance members, but not to its static members. Callers rarely need to name the types, because inference works them out, though they can write Util.<String>firstOr(list, "x").

Why this question is asked

Generic methods and generic classes look alike but differ in where their type parameters live, and that difference explains a whole family of compile errors, such as trying to use T in a static method. Interviewers ask this coding question to check that we can write both forms correctly and say precisely where each type parameter is declared and visible. In this answer, we will recap type parameters, write firstOr step by step, write Pair, compare their scopes, and see how type inference and explicit type arguments work.

1A Quick Recap of Type Parameters

A type parameter is a placeholder for a type, written as a name such as T, that the caller fills in. List<E> has the type parameter E; when we write List<String>, String is the type argument. Type parameters let one piece of code work with many types while the compiler still checks each use.

There are two places to declare type parameters. A generic type, a class, interface or record, declares them after its name: class Box<T>. A generic method declares them before its return type: static <T> T pick(T a, T b). The place of the declaration decides where the parameter can be used.

2Writing firstOr

We want a method that returns the first element of a list, or a fallback value if the list is empty, for a list of any element type.

Java
final class Util {
static <T> T firstOr(List<? extends T> list, T fallback) {
return list.isEmpty() ? fallback : list.get(0);
}
}
String a = Util.firstOr(List.of("x", "y"), "none"); // x
String b = Util.firstOr(List.of(), "none"); // none
Number n = Util.firstOr(List.of(1, 2), 0.5); // 1: T is inferred as a common supertype of Integer and Double

The <T> before the return type is the declaration. It says this method has its own type parameter T, which exists only for this method and is chosen fresh at every call. The return type T and the parameter T fallback then use it.

Notice the parameter type List<? extends T> rather than List<T>. The method only reads from the list, so it can accept a list of any subtype of T. That is what lets the last call work: the list holds Integers and the fallback is a Double, so the compiler picks T as a type both fit into, here a supertype such as Number, and the list of Integers is accepted as a List<? extends Number>. With List<T>, that call would not compile, because a List<Integer> is not a List<Number>.

3Writing Pair

For a pair of two values of possibly different types, a record is the shortest correct form. The type parameters are declared after the record's name.

Java
record Pair<A, B>(A first, B second) {
<C> Pair<A, C> withSecond(C value) { return new Pair<>(first, value); } // a generic method inside a generic type
// static A empty() { ... } // error: non-static type variable A cannot be referenced from a static context
static <A, B> Pair<A, B> of(A a, B b) { return new Pair<>(a, b); } // a static method declares its own
}
var p = Pair.of("age", 42); // Pair<String, Integer>
String k = p.first(); // age
var q = p.withSecond(true); // Pair<String, Boolean>
Where type parameters are declared and visible
DeclarationWrittenVisible in
Generic methodstatic <T> T firstOr(...)that method only, chosen per call
Generic class or recordrecord Pair<A, B>(...)all instance members of the type
Static method in a generic typestatic <A, B> Pair<A, B> of(...)its own parameters; the class's are not visible
Instance method adding its own<C> Pair<A, C> withSecond(C c)A, B from the type and its own C
The angle brackets come before the return type for methods and after the name for types.

A and B belong to each Pair instance. Every instance member, its fields, accessors and instance methods such as withSecond, can use them, because each instance has concrete types for them: p is a Pair<String, Integer>. An instance method can also add its own type parameter, like C in withSecond.

Static members cannot use A or B. A static method belongs to the class, not to any instance, so there is no particular Pair<A, B> whose A it could mean; at the class level there is only the one class Pair. That is why the static factory of declares its own <A, B>. They happen to have the same names, but they are new type parameters of the method, which is a common source of confusion.

4Type Inference and Explicit Type Arguments

At almost every call, the compiler infers type arguments from the argument types and from the target type, the type the result is assigned to. That is why Util.firstOr(List.of("x"), "none") needs no annotation. For a constructor, the diamond <> asks for the same inference, as in new Pair<>(first, value).

Sometimes inference has nothing to go on, or picks a type we do not want. Then we can pass type arguments explicitly, written between the dot and the method name.

Java
List<String> empty = List.of();
Object o = Util.firstOr(empty, null); // T inferred as String here
var s = Util.<String>firstOr(List.of(), "x"); // explicit: T is String
// var t = <String>firstOr(List.of(), "x"); // error: explicit arguments need a qualifier such as Util. or this.

Notice the explicit form needs something before the dot, a class name for static methods or this for instance methods. Explicit type arguments are rarely needed in modern Java, since inference improved a lot in Java 8, but they are useful when a chained call loses the target type or when we want to force a wider type.

5Follow-up questions

Why use List<? extends T> instead of List<T> in firstOr?

Because the method only reads from the list. ? extends T accepts a list of T or any subtype of T, which makes the method usable with more argument combinations, such as a List<Integer> with a Double fallback. It is the producer-extends half of the PECS rule.

Could Pair be a class instead of a record?

Yes: final class Pair<A, B> with two final fields, a constructor, accessors, equals, hashCode and toString. The record generates all of that and declares its type parameters in the same place, after the name.

Can a type parameter be shadowed by a method's type parameter?

Yes. If a generic class Box<T> declares an instance method <T> void put(T t), the method's T is a different, new type parameter that hides the class's T inside that method. It compiles, but it is confusing, so we pick a different letter.

6Conclusion

In this answer, we wrote static <T> T firstOr(List<? extends T> list, T fallback) and record Pair<A, B>(A first, B second). A generic method declares its type parameters before the return type and they are scoped to that method, chosen fresh at each call. A generic type declares them after its name, and they are available to instance members but not static ones, which must declare their own. Inference usually supplies the type arguments, and Util.<String>firstOr(...) passes them explicitly when needed.

What to say in the interviewAbout 80 seconds aloud

Open with

For the method I would write static <T> T firstOr(List<? extends T> list, T fallback), returning list.isEmpty() ? fallback : list.get(0). The <T> before the return type is where the type parameter is declared, and it is scoped to that one method, so each call gets its own T.

Then, if they want more

I use List<? extends T> rather than List<T> because I only read from the list, so it can be a list of any subtype of T; for example a list of Integers with a Double fallback works with T inferred as a common supertype like Number. For the pair, record Pair<A, B>(A first, B second) {}.

Here the type parameters are declared after the type's name, and they belong to each instance, so all instance members, the fields, accessors and instance methods, can use A and B.

Static members cannot, because a static method belongs to the class, not to a particular Pair<String, Integer>, so a static factory has to declare its own, like static <A, B> Pair<A, B> of(A a, B b).

Callers almost never need to state type arguments, because inference works them out from the arguments and the target type, but they can write Util.<String>firstOr(list, "x") when inference has nothing to go on or picks something they do not want.

About this answer

Last updated
12 November 2026
Versions it names
Java 8
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Write a generic method that returns the first element of any list or a default value...": https://scrivio-interview-library.netlify.app/java/generics/77f988313a/