jeudi 3 septembre 2020

Unusual syntax used for object construction

I'm new to Java programming. While reading through the code of an open source project, I came across a line of code which I can't understand:

final Type typeOfMap = new TypeToken<Map<String, Object>>() {}.getType();

My questions are:

  1. I usually call a constructor like this: final Type typeOfMap = new TypeToken<Map<String, Object>>(). I have never seen it followed by other pieces of code such as {}.getType(). What kind of syntax is this?
  2. Is {} an object? Why can you call a function on it?

P.S. Type is java.lang.reflect.Type, and TypeToken is com.google.gson.reflect.TypeToken.





Aucun commentaire:

Enregistrer un commentaire