Passing a generic class to a java function? -


Is it possible to use generic when passing a class for Java function?

I was hoping to do something like this:

Public Stable Class DoStuff {public & lt; T class & lt; List & gt; & Gt; Zero Dust Tolist (T Classname) {System.out.println (className); } Public Zero Testing () {doStuffToList (List.class); // blankets Dustfaltist (Classes); // Compiler Error (unwanted behavior) doStuffToList (integer class); // compiler error (desired behavior)}}

Ideally the list. The square and ArrayList.class line will work fine, but the Integer.class line will cause a compile error. I can use the class because my class list instead of T ; & Gt; , but then I will not catch the above version Integer.class.

  public & lt; T increases the list & gt; Void doStuffToList (class  clazz)  

You're passing after all the class - parameter type class ,

Actually, & lt; T increases the length of the square .. .. & gt; means T == class , because class is final . And then you can fix class type parameters to list - any list , just list . Therefore, if you want your example to work, then you will need it:

  public & lt; T class increases ?? List & gt; & Gt; Void doStuffToList (T Klase)  

But this is not necessary at all.


Comments