what's the meaning of Class<?> and how to use it

K

keepiss

The return type of JTable.getColumnClass(int column) is Class<?>. What
is the meaning of it and how to use it properly? It seems the following
code is okay,

Class c = jTable1.getColumnClass(0);

Is there another way to use Class<?>?
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

keepiss schreef:
The return type of JTable.getColumnClass(int column) is Class<?>. What
is the meaning of it and how to use it properly? It seems the following
code is okay,

Class c = jTable1.getColumnClass(0);

Is there another way to use Class<?>?

It is used intensively in reflection. I suggest you keep away from that
a while until you feel you have enough experience to delve into the topic.

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html

H.
- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFETKaae+7xMGD3itQRAg1cAJwIGLARSUg6A3p6wYHzLjyAUzjAVQCcDi7L
IymtBSSFgK2vgf99/JjeK1k=
=qXMO
-----END PGP SIGNATURE-----
 
K

keepiss

I know 'Class' is mainly used in reflection. What I don't understand is
<?>. What does it mean?
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Thomas Fritsch schreef:
An attempt to explain <?> is given in
<http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf> (chapter "4
Wildcard").
May be you can make a sense of it. (I don't, but I'm new to generics
anyway :-( )

The <T> in Class<T> is the class it actually represents. So the Class
object belonging to YourClass is Class<YourClass>. This has all kinds
of advantages in making sure the right class object is given in method
arguments etc.

If a method returns Class<?>, this means it can return any Class object.

H.
- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFETLoge+7xMGD3itQRAtxXAJ9XXVlRWQZKfGMgJiBsv//xQmLRwgCfeffs
ZCLY/Mo/gPTJmNtxLjDiT6Y=
=0+lT
-----END PGP SIGNATURE-----
 
T

Thomas Fritsch

Hendrik said:
Thomas Fritsch schreef:


The <T> in Class<T> is the class it actually represents. So the Class
object belonging to YourClass is Class<YourClass>. This has all kinds
of advantages in making sure the right class object is given in method
arguments etc.

If a method returns Class<?>, this means it can return any Class object.
Ah, thanks! So Class<?> is pretty much the same as Class.

A somewhat related question:
How do I get the concrete semantic meaning of a <T> or <E> parameter
found in the javadoc class declaration? For example in:
public class Class<T> extends Object

public interface Collection<E> extends Iterable<E>

Surely we can make clever guesses about what the <T> or <E> parameter in
a particular class is intended to mean, by reading the javadoc of the
methods of that class. But shouldn't there be a more formal description,
too? Might be as a new javadoc tag, for example:
public class Class<T> extends Object
@genericparam T this class

public interface Collection<E> extends Iterable<E>
@genericparam E the type of objects contained in this Collection
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Thomas Fritsch schreef:
Ah, thanks! So Class<?> is pretty much the same as Class.

Well, yes, it is the generified version. Class is the erasure type.
Read up on generics if this sounds unclear to you.

Also see
http://www.angelikalanger.com/Gener...type parameter of class java.lang.Class mean?
shortened: http://tinyurl.com/hk39y
A somewhat related question:
How do I get the concrete semantic meaning of a <T> or <E> parameter
found in the javadoc class declaration? For example in:
public class Class<T> extends Object

public interface Collection<E> extends Iterable<E>

You (almost) can’t. The information is erased. At runtime, it is an
Object. For details and tricks, browse the above site.
Surely we can make clever guesses about what the <T> or <E> parameter in
a particular class is intended to mean, by reading the javadoc of the
methods of that class. But shouldn't there be a more formal description,
too? Might be as a new javadoc tag, for example:
public class Class<T> extends Object
@genericparam T this class

public interface Collection<E> extends Iterable<E>
@genericparam E the type of objects contained in this Collection

Most of the time, the E will be unknown at the moment the class is
written, thus nothing sensible can be said about it. Everything you
mention here is indeed in the documentation, and is sort of ‘evident’ as
it is the purpose of generics. Class is the noticeable exception.

H.
- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFETNT2e+7xMGD3itQRAmiSAJ92pSw151tWVa9qVeK0/e4VqL0isgCfV1WE
6P1qXSqguJQyprJY2xPJ5W8=
=nRiv
-----END PGP SIGNATURE-----
 
T

Thomas Fritsch

Hendrik said:
Thomas Fritsch schreef:

Most of the time, the E will be unknown at the moment the class is
written, thus nothing sensible can be said about it. Everything you
mention here is indeed in the documentation, and is sort of ?evident? as
it is the purpose of generics. Class is the noticeable exception.
The other noticeable exception from self-evidence is
public abstract class Enum <E extends Enum<E>>
But AFAIK that class is only for internal use by the compiler (as superclass
of all enum types). Hence incomprehensibility is no problem here. ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top