Reification
This lesson explains reifiable types in Java.
We'll cover the following...
Question # 1
What is reification in Java?
Reify is defined as to make something more real or consider it as real. Turn abstract into concrete or tangible. In Java terminology reify means the explicit representation of a type at runtime. As mentioned earlier the types ArrayList<Integer>
, ArrayList<String>
and ArrayList<ArrayList<Integer>>
are essentially just the raw type ArrayList
at runtime. There's no information at runtime available to ascertain whether an arraylist was declared as an arraylist of integers or was it an arraylist of strings. This lack of type information at runtime makes generic types non-reifiable types. Types that in a way can't remember, retain or convert compile time type information into runtime type information. The type information is lost because of erasure. Part of the reason to implement generics using erasure was compatibility. So that the new version of Java continues to work with Java programs written before generics and these programs can also be retrofitted with generic code.
Generics enforce type constraints at compile time. The following snippet will fail to compile
Non-reifiable Type
List<Integer> list = new ArrayList<>();
// Compile time error
List<Object> refToList = list;
// Never happens when running the program
refToList.add(new Double(5));