I was looking into the Guava library and I came across an empty anonymous inner class in TypeToken.
TypeToken<List<String>> stringListTok = new TypeToken<List<String>>() {};
What is exactly use of empty anonymous inner class and what are the useful scenarios where it can be helpful?
The purpose of this is to allow
TypeTokento find the superclass of the instance – which will preserve the type argument information.For example, although an
ArrayList<String>object doesn’t know that that its element type isString, due to erasure, the superclass information is not lost, sonew ArrayList<String>{}knows that its superclass isArrayList<String>, not justArrayList.TypeTokenuses that technique to represent a constructed generic type.