Class<T> class

Hi,I was just wondering what the purpose of making this class generic is ...Any examples ?Thanks,Adrian
[138 byte] By [AdrianSosialuka] at [2007-11-27 4:01:31]
# 1
If we specify the type we dont need to cast for the original object we need as return type in most of the Class methods..
dinezhra at 2007-7-12 9:06:15 > top of Java-index,Core,Core APIs...
# 2

The first good reason that springs to mind is the lack of an explicit cast (Foo) in:

Class<Foo> cls = Foo.class;

Foo f = cls.newInstance()

thus preventing mistakes like the following (pre-Generics)

Class cls = Date.class;

String s = (String) cls.newInstance();

Even if you don't know what sort of class you have, the Class.asSubclass method can be used in a nice way:

Class<?> cls = Class.forName(someFooSubclassName);

// The following is checked at runtime

Class<? extends Foo> fooCls = cls.asSubclass(Foo.class);

Foo f = fooCls.newInstance();

And not an 'unchecked' warning in sight...

Alex

AlexHudsa at 2007-7-12 9:06:15 > top of Java-index,Core,Core APIs...
# 3
Thanks a lot guys !I had a quick look through the API and I couldn't find any method that wouldreturn type T - newInstance() had hidden at the very bottom ;)Special thanks to Alex for a very nice explanation of Class.asSubclass() method ;)Cheers,Adrian
AdrianSosialuka at 2007-7-12 9:06:15 > top of Java-index,Core,Core APIs...