0

I'm looking at the documentation of JasperReports and I do not understand the return type of the following method:

public java.lang.Class<?> getValueClass()
Alex K
  • 22,315
  • 19
  • 108
  • 236

3 Answers3

3

The method returns a Class object. The ? indicates that it can be any type of class. You can read more about wildcards here. These Class objects are often utilized when you're dealing with reflection.

Community
  • 1
  • 1
arshajii
  • 127,459
  • 24
  • 238
  • 287
1

Class<?> refers to any instance of Class. As compared to Class<? extends Collection> which would narrow the criteria down to a limited group of classes (those that extend Collection).

This is particularly important when calling methods like newInstance. If you have Class<?> a and call a.newInstance() you'll get an Object. if you have Class<? extends Collection> and call b.newInstance() you'll get an instance of Collection.

Donald.McLean
  • 869
  • 12
  • 18
  • Thank you for your explanation. I have one more followup question: why not simply use Class as a return type instead of Class> – user1700440 Sep 26 '12 at 13:58
  • I think I already know the answer to that one. Since one wants to define more precisely the return type of some Class methods (such as newInstance), the Class got parameterized with a wildcard type ?. – user1700440 Sep 26 '12 at 14:13
  • You are correct: `Class` and `Class>` are synonymous, but `Class>` is more precise - it tells the reader that you've actually thought about what kind of `Class` it might be. – Donald.McLean Sep 26 '12 at 14:16
0

It just returns an instance of a class. ? parameter which represents a generic wild card object i.e it is a class of any type.

sakthisundar
  • 3,278
  • 3
  • 16
  • 29