Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiating generics type in java

Tags:

java

generics

I would like to create an object of Generics Type in java. Please suggest how can I achieve the same.

Note: This may seem a trivial Generics Problem. But I bet.. it isn't. :)

suppose I have the class declaration as:

public class Abc<T> {
    public T getInstanceOfT() {
       // I want to create an instance of T and return the same.
    }
}
like image 265
Mohd Farid Avatar asked Mar 12 '10 16:03

Mohd Farid


People also ask

Can you instantiate generic class?

The usual solution to this problem in Java is to use a Class<T> object as a type token, and then use that to reflectively instantiate the object you need.

How do you declare a generic type in Java?

To update the Box class to use generics, you create a generic type declaration by changing the code "public class Box" to "public class Box<T>". This introduces the type variable, T, that can be used anywhere inside the class. As you can see, all occurrences of Object are replaced by T.

Can you instantiate objects using a type parameter?

Since the type parameter not class or, array, You cannot instantiate it.


3 Answers

public class Abc<T> {
    public T getInstanceOfT(Class<T> aClass) {
       return aClass.newInstance();
    }
}

You'll have to add exception handling.

You have to pass the actual type at runtime, since it is not part of the byte code after compilation, so there is no way to know it without explicitly providing it.

like image 70
Jens Schauder Avatar answered Oct 06 '22 19:10

Jens Schauder


What you wrote doesn't make any sense, generics in Java are meant to add the functionality of parametric polymorphism to objects.

What does it mean? It means that you want to keep some type variables of your classes undecided, to be able to use your classes with many different types.

But your type variable T is an attribute that is resolved at run-time, the Java compiler will compile your class proving type safety without trying to know what kind of object is T so it's impossible for it to let your use a type variable in a static method. The type is associated to a run-time instance of the object while public void static main(..) is associated to the class definition and at that scope T doesn't mean anything.

If you want to use a type variable inside a static method you have to declare the method as generic (this because, as explained type variables of a template class are related to its run-time instance), not the class:

class SandBox
{
  public static <T> void myMethod()
  {
     T foobar;
  }
}

this works, but of course not with main method since there's no way to call it in a generic way.

EDIT: The problem is that because of type erasure just one generic class is compiled and passed to JVM. Type checker just checks if code is safe, then since it proved it every kind of generic information is discarded.

To instantiate T you need to know the type of T, but it can be many types at the same time, so one solution with requires just the minimum amount of reflection is to use Class<T> to instantiate new objects:

public class SandBox<T>
{
    Class<T> reference;

    SandBox(Class<T> classRef)
    {
        reference = classRef;
    }

    public T getNewInstance()
    {
        try
        {
            return reference.newInstance();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return null;
    }

    public static void main(String[] args)
    {
        SandBox<String> t = new SandBox<String>(String.class);

        System.out.println(t.getNewInstance().getClass().getName());
    }
}

Of course this implies that the type you want to instantiate:

  • is not a primitive type
  • it has a default constructor

To operate with different kind of constructors you have to dig deeper into reflection.

like image 14
Jack Avatar answered Oct 06 '22 19:10

Jack


In the code you posted, it's impossible to create an instance of T since you don't know what type that is:

public class Abc<T>
{
       public T getInstanceOfT()
       {
           // There is no way to create an instance of T here
           // since we don't know its type
       }
} 

Of course it is possible if you have a reference to Class<T> and T has a default constructor, just call newInstance() on the Class object.

If you subclass Abc<T> you can even work around the type erasure problem and won't have to pass any Class<T> references around:

import java.lang.reflect.ParameterizedType;

public class Abc<T>
{
    T getInstanceOfT()
    {
        ParameterizedType superClass = (ParameterizedType) getClass().getGenericSuperclass();
        Class<T> type = (Class<T>) superClass.getActualTypeArguments()[0];
        try
        {
            return type.newInstance();
        }
        catch (Exception e)
        {
            // Oops, no default constructor
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args)
    {
        String instance = new SubClass().getInstanceOfT();
        System.out.println(instance.getClass());
    }
}

class SubClass
    extends Abc<String>
{
}
like image 28
Martin Avatar answered Oct 06 '22 18:10

Martin