Can we instanciate the type parameters?

Type parameters can not be instantiated.
Example:
Class Gener{
T obj;
Gener(T o)
{obj=new T();//error}}
Restriction on static members:
No static member can use a type parameter declared by the enclosing class
class Gen
{
static T obj;
static void getobj(){System.out.println(obj);}
};
Its compilation error like this
demo.java:3: non-static class T cannot be referenced from a static context
static T obj;
^
1 error
Although you can not declare static members that use a type parameter declared by the enclosing class, you can declare your static generics methods, which define their own type parameter.

No comments:

Post a Comment