-1

While looking into some code I found out that there is following kind of syntax.

 protected <T> T Execute(Class<T> returnType){
    T t;
    return t;
    }

What does this mean?? What if I want to save the outcome in some variable of other class?

thisisdude
  • 543
  • 1
  • 7
  • 31

2 Answers2

2

Type parameter has been added to java.lang.Class to enable one specific use of Class objects as type-safe object factories. Essentially, the addition of lets you instantiate classes in a type-safe manner, like this:

T instance = myClass.newInstance();

How to use Class<T> in Java?

Community
  • 1
  • 1
Pooja Arora
  • 574
  • 7
  • 19
1

You can use newInstance() method.

protected <T> T execute(Class<T> returnType) {
    T t = returnType.newInstance();
    return t;
    }

But you will have to handle

InstantiationException, IllegalAccessException

Although this is a strange, unwanted way to create new objects, AbstractFactory would be a better solution.

Beri
  • 11,470
  • 4
  • 35
  • 57