Quick revision
The short answer
Heap pollution is when a variable of a parameterised type, such as List<String>, refers to an object that is not of that type, for example a list holding an Integer. Generic varargs cause it because T... or List<String>... is created as an array of the erased type, so through aliasing that array can be filled with the wrong kind of list, and javac warns about this at the declaration and at call sites. @SafeVarargs is safe when the method neither stores anything into the varargs array nor lets a reference to it escape to untrusted code, and it is allowed only on static, final and private methods and on constructors, because an overriding method could break the promise.
What to remember
In this answer, we saw that heap pollution is a variable of a parameterised type referring to an object of the wrong type, which only happens through unchecked operations and fails later at a compiler-inserted cast. Generic varargs create it because the varargs array is created with the erased type, so aliasing can fill it with the wrong lists, and javac warns at the declaration and at call sites. @SafeVarargs is honest when the method never stores into the array and never lets it escape, and it is only allowed on static, final and private methods and constructors, since overriding could break the promise.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Heap pollution is when a variable of a parameterised type, such as List<String>, refers to an object that is not of that type, for example a list holding an Integer. Generic varargs cause it because T... or List<String>... is created as an array of the erased type, so through aliasing that array can be filled with the wrong kind of list, and javac warns about this at the declaration and at call sites. @SafeVarargs is safe when the method neither stores anything into the varargs array nor lets a reference to it escape to untrusted code, and it is allowed only on static, final and private methods and on constructors, because an overriding method could break the promise.
Why this question is asked
Varargs warnings are among the most common warnings in Java code, and many teams silence them without knowing what they mean. Interviewers ask about heap pollution and @SafeVarargs to see whether we understand the risk behind the warning and can tell a safe varargs method from a dangerous one. In this answer, we will define heap pollution with a small example, see how generic varargs create it, read the warnings javac gives, and then learn the exact conditions under which @SafeVarargs is honest and where it is allowed.
1What Heap Pollution Means
Generics are checked by the compiler and then erased, so at run time a List<String> is just a List. As long as all code respects the types, every List<String> really holds strings. Heap pollution is the situation where that is no longer true: a variable of a parameterised type points to an object whose contents do not match its type. It can only happen through unchecked operations, such as raw types or unchecked casts, and the compiler warns at each of them.
Notice where it fails: at the read, through a cast the compiler inserted, far from the line that caused the problem. That delay is what makes heap pollution expensive to debug.
2How Generic Varargs Create It
A varargs parameter is an array. When we declare static <T> void m(T... items), the compiler creates an array at every call site to hold the arguments. For a type parameter T, or a parameterised type like List<String>, it cannot create a T[] or a List<String>[], because those types are erased; it creates an array of the erasure instead, such as Object[] or List[]. So inside the method, the parameter has a static type that is more specific than the real array. If the method stores into that array through another reference, or hands the array to code that does, the array can be polluted.
- Call m(List.of("a"))with
m(List<String>... lists) - Compiler creates a List[]
List<String>[] cannot be created - Method stores through an aliasObject[] objs = lists
- Wrong list storedstore check sees only List
- Later read fails
ClassCastExceptionat an inserted cast
5 steps in order. Play them, or step through them one at a time.
The method looks well typed, yet a List<Integer> ends up in a List<String>[]. The second common danger is returning the array: static <T> T[] toArray(T... a) { return a; } returns an Object[] when called from another generic method, which then fails at a compiler-inserted cast in a caller that expected a String[].
3The Warnings javac Gives
javac warns in two places. At the declaration of a method with a non-reifiable varargs parameter type, it says the method has possible heap pollution from parameterized vararg type. At each call site it gives an unchecked generic array creation warning for the varargs parameter. The call-site warning is the annoying one: a library method like Arrays.asList or List.of would produce warnings in every caller's code if the JDK had not annotated it.
Both warnings are honest. The compiler cannot see whether the method body is safe, so it warns everyone. @SafeVarargs exists to let the method's author, who can see the body, promise that it is safe and remove the warnings for all callers.
4When @SafeVarargs Is Honest
The promise has two parts. First, the method does not store anything into the varargs array; it only reads the elements. Second, it does not let a reference to the array escape to code that could store into it or treat it as a more specific type: it does not return the array, assign it to a field, or pass it to an untrusted method. Passing it to another @SafeVarargs method, or reading it into a new collection, is fine.
The first method is safe and gets the annotation. The second is not: it returns the array, so callers may receive an Object[] typed as T[]. Annotating it hides a real bug, and the compiler cannot catch the lie.
5Where @SafeVarargs Is Allowed
The annotation is a promise about the method body. If a subclass could override the method, the overriding body might store into the array and break the promise, while callers through the parent type still see no warning. So the compiler only allows @SafeVarargs on methods that cannot be overridden: static methods, final instance methods, constructors, and, since Java 9, private instance methods. On an ordinary instance method it is a compile error.
| Method kind | Allowed? | Reason |
|---|---|---|
| static method | Yes | cannot be overridden |
| final instance method | Yes | cannot be overridden |
| constructor | Yes | not inherited |
| private instance method | Yes, since Java 9 | cannot be overridden |
| other instance method | No | an override could break the promise |
| interface default method | No | can be overridden |
6Follow-up questions
Is @SuppressWarnings("unchecked") the same as @SafeVarargs?
No. @SuppressWarnings on the method only hides the warning at the declaration; every caller still gets the unchecked generic array creation warning. @SafeVarargs removes the call-site warnings too, which is why library methods use it, and it states a specific, checkable promise about the array.
Which JDK methods use @SafeVarargs?
Several, including Arrays.asList, List.of with many arguments, Set.of, Stream.of and EnumSet.of. They only read the array or copy it, which is why they can promise safety.
Does heap pollution always end in an exception?
Not necessarily. If the polluted value is only passed around as Object or printed, nothing fails. That is part of the danger: the bad value can travel far and fail much later, or corrupt data quietly.
7Conclusion
In this answer, we saw that heap pollution is a variable of a parameterised type referring to an object of the wrong type, which only happens through unchecked operations and fails later at a compiler-inserted cast. Generic varargs create it because the varargs array is created with the erased type, so aliasing can fill it with the wrong lists, and javac warns at the declaration and at call sites. @SafeVarargs is honest when the method never stores into the array and never lets it escape, and it is only allowed on static, final and private methods and constructors, since overriding could break the promise.
Open with
Heap pollution is when a variable of a parameterised type points to an object that is not of that type, for example a List<String> that actually contains an Integer.
Then, if they want more
It can only happen through unchecked operations like raw types or unchecked casts, and it usually fails much later, at a cast the compiler inserted, which makes it hard to debug.
Generic varargs are a common source because a varargs parameter is an array, and for a type like T... or List<String>... the compiler cannot create the real generic array, so it creates an array of the erasure, Object[] or List[].
If the method stores into that array through an alias, or returns it or keeps it in a field, wrong values can get in or the array can be seen as the wrong type. So javac warns about possible heap pollution at the declaration and about unchecked generic array creation at every call site.
I use @SafeVarargs when the method only reads the elements and never stores into the array or lets it escape, like a method that copies the arguments into a new list. It is only allowed on methods that cannot be overridden: static, final, private since Java 9, and constructors, because an override could break the promise.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 9
- 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.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 "What is heap pollution, how do generic varargs cause it, and when is it safe to annotate...": https://scrivio-interview-library.netlify.app/java/generics/b35d7f1823/