The {}
is the body of an anonymous class.
If you wanted to make it more readable, this is what it could look like:
class MyTypeToken extends TypeToken<Collection<Data>> { }
TypeToken<Collection<Data>> tcd = new MyTypeToken();
Type type = tcd.getType();
Here is a breakdown of the more succinct shorthand you have used:
Type type =
new TypeToken<Collection<Data>>() { // this will initialise an anonymous object
/* in here you can override methods and add your own if you need to */
} /* this ends the initialisation of the object.
At this point the anonymous class is created and initialised. */
.getType(); // here you call a method on the object
I prefer using this:
TypeToken<Collection<Data>> tt = new TypeToken<Collection<Data>>(){ };
Type type = tt.getType();
One more thing I want to mention is that anonymous classes do not have the same class type as other anonymous classes which inherit from the same parent.
So if you have the following:
TypeToken<Collection<Data>> tt1 = new TypeToken<Collection<Data>>(){ };
TypeToken<Collection<Data>> tt2 = new TypeToken<Collection<Data>>(){ };
tt1.getClass() == tt2.getClass()
will never be true.