Why does the following method not create potential heap contamination?

Java 7

I wrote this:

public static <E extends Enum<E>> List<SelectItem> getSelectItemList(Enum<E>... es){ List<SelectItem> items = new ArrayList<SelectItem>(); for(Enum<E> e : es){ items.add(new SelectItem(e, e.toString())); } return items; } 

and this method compiled without warning. What for? I expected that using varargs of a typical type (which is actually an array) creates

Potential heap pollution via varargs parameter es

Could you explain this?

+3
source share
1 answer

Variations of non-recoverable types tend to work as follows. Enum<E> ... effectively evaluated as Enum[] at runtime. At this point, you cannot be sure which links can fall into the array.

More information here: https://docs.oracle.com/javase/tutorial/java/generics/nonReifiableVarargsType.html

The basic type of Enum will actually give you some type of security, even at compile time, which is why I think that's why you are safe.

+2
source

All Articles