J.III · Java 25 practice question
A generic class Cache<K, V> declares a method V lookup(K key). In the parameterized type Cache<String, Integer>, how is the type of lookup determined?
Check answer and explanation
Correct answer: By substituting K with String and V with Integer in lookup's declared type
Explanation
For a parameterized type C<T1,...,Tn> with non-wildcard type arguments, the type of a member m is its declared type with each type parameter Ai substituted by Ti.
Common trap
Erasure happens during compilation to bytecode, not when determining the static member type of a parameterized type. Type arguments in a parameterized type are resolved at compile time via substitution, not left unbound until runtime.
Source: JLS SE 25 §4.5.2. Members and Constructors of Parameterized Types
Checked against the cited text on 2026-11-25.