2
public class GenericClass<T> {

    private final Class<T> clazz;

    public GenericClass(Class<T> clazz) {
        this.clazz = clazz;
    }

}

public Test extends GenericClass<Person.class> {

    public Test() {
        super(Person.class);
    }

}

in the Generic class, I will be passing clazz (in this case Person.class) as an argument in one of the methods, but now I want to pass clazz[] (in this case Person[].class) in another method inside Generic class. How do I do that? how can I get clazz[] from clazz i.e., Person[].class from Person.class

Shiva
  • 913
  • 2
  • 8
  • 7

1 Answers1

2

The best answer I've ever been able to find is

 Array.newInstance(myClass, 0).getClass()
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • I am calling rest template in the Generic class and want to pass Class type in the methods. for example restTemplate.exchange(url, HttpMethod.GET, requestEntity, this.clazz, params); and restTemplate.exchange(url, HttpMethod.GET, requestEntity, this.clazz[], params); is there any way to pass the generic array of class in the method (in this case Person[].class) I couldn't able to figure out how Array.newInstance(myClass, 0).getClass() works – Shiva Apr 01 '16 at 14:38
  • @Shiva this answer gets you `Person[].class` if you just put it in. – Louis Wasserman Apr 01 '16 at 17:09
  • got it. It works now I had to cast it Class arrayClass = (Class) Array.newInstance(this.clazz, 0).getClass(); Thanks @Louis – Shiva Apr 01 '16 at 19:40