jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes java Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "java Generics" Watch "java Generics" New topic

java Generics

Mamatha Achuthan

Joined: Oct 09, 2011
Posts: 8


In some part of my code I need to invoke a third party function which has a signature like this

public static <T> T[] functionToProcess(T[][] a_rrs, Class<T[]> a_destType)

From the signature I make out that the function accepts a generic 2-dimension array and a Class object referring to single dimension array of the same generic type as parameters.

I tried to invoke the function like this

and received an error.

Any idea where is has gone wrong?


Marcin Kwiatkowski
Ranch Hand

Joined: Aug 06, 2007
Posts: 32


Take a look at getClass() method documentation. Why don't you use File[].class instead?
Nomaan Butt
Ranch Hand

Joined: Oct 19, 2011
Posts: 54
can you post the exception
Matthew Brown

Joined: Apr 06, 2010
Posts: 4338

It looks like you're calling theoutput.getClass() before you've defined theoutput, so that's never going to work. Marcin's suggestion should fix that - you might as well use a class literal if you can.

One further point - casting the return value to File[] is unnecessary - the return type is T[], so it will already be File[] in that call.
Mamatha Achuthan

Joined: Oct 09, 2011
Posts: 8


The File[].class works! Thanks.

The Netbeans IDE was showing the following error message when I used the getClass function

Method cannot be applied to given types
found : java.io.File[][],java.lang.Class<capture #1 of ? extends java.io.File[]>
required : T[][],java.lang.Class(T[])

Thanks again,

Rob Spoor

Joined: Oct 27, 2005
Posts: 19649

That's because getClass() returns a Class<?>, without any generic information. The main way to get a Class object with generic information is to use a class literal. Class.getSuperclass and Class.asSubclass return a Class object with some generic information but it still has a wildcard type.

How To Ask Questions How To Answer Questions
Mamatha Achuthan

Joined: Oct 09, 2011
Posts: 8


Thanks for the information you all have shared. Thanks to you all, I took a look the 'Class' class and leaned new things.

Thanks again,

I agree. Here's the link: http://aspose.com/file-tools
subject: java Generics
Similar Threads
Arrays.asList odd behaviour
Vararg Generic
stuck up in the problem
Custom tags in jsp - .tld file doubt
Passing array in an EL taglib function