Click here to Skip to main content
15,916,379 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Consider a class
Java
class Person(){
   private int age;

   public int getAge(){
       return age;
   }

}

To access the method getAge() we can directly call it as
Java
new Person().getAge();


What I have tried:

but while using Typetoken Class Why am i supposed to call the method getType() like this.
Java
new TypeToken<List<T>>(){}.getType();

Why should i put flower braces. what is the reason.
Posted
Updated 12-Oct-17 4:07am

1 solution

The curly braces there are to provide a default value for the new instance, and in this case are irrelevant as there is no initialization data in it so they can be omitted.

They are often seen in lists, arrays, and so forth:
C#
List<string> names = new List<string>() { "John", "Mike", "Susan" };
 
Share this answer
 
Comments
Member 13461301 13-Oct-17 3:31am    
But when i am not using the flower braces. The IDEshows error that the TypeToken has protected access.
the line shows error `new TypeToken<List<t>>().getType();`
But this `new TypeToken<List<t>>(){}.getType();` doesn't.
This class is from gson library.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900