• Post Reply Bookmark Topic Watch Topic
  • New Topic

Meaning of 'Class<>'  RSS feed

 
Abhay Bhatt
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per my readings/understandings:-

Every time JVM creates an object, it also creates a java.lang.Class object that describes the type of the object. All instances of the same class share the same Class object.

Class<SomeClass> is a way to represent this object, which is of type Class and was created when any object of type 'SomeClass' class was created(as per above definition) and also, is associated with 'SomeClass' class.

But when I read below StackOverflow thread, regarding Class<?>:-

https://stackoverflow.com/questions/9921676/what-does-class-mean-in-java

it is said in the very first answer that by writing Class<?>, we're declaring a Class object which can be of any type. I am so confused, wouldn't this object be of type Class regardless of what '?' is, since its an object of class Class?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhay Bhatt wrote:
it is said in the very first answer that by writing Class<?>, we're declaring a Class object which can be of any type. I am so confused, wouldn't this object be of type Class regardless of what '?' is, since its an object of class Class?


First of all, you are declaring a reference, and not the object. Wildcards are for the reference only, and not related to the object. Second, the type being referred to here is the generic, and not the type of the object... but yes, you are declaring a reference to a Class instance, whose generic can be any type (assuming your code, as you haven't shown that to us).

Henry
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:. . . a Class instance, whose generic can be any type . . .
I forget where I read it, but I have memories of reading that Class<?> clazz; means, “I want a Class object representing a particular type, but haven't yet told you which type.”
 
Abhay Bhatt
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:

First of all, you are declaring a reference, and not the object.



Do you mean a reference to an object by the term 'reference' here?

Henry Wong wrote:

Wildcards are for the reference only, and not related to the object.



Can you please elaborate?

Henry Wong wrote:

Second, the type being referred to here is the generic



I am unable to understand what 'generic' means. Can you please explain?
 
Knute Snortum
Sheriff
Posts: 4273
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhay Bhatt wrote:
Henry Wong wrote:
Second, the type being referred to here is the generic

I am unable to understand what 'generic' means. Can you please explain?

Think about Lists for a second.  Before generics, a List was a list of anything.  It could be Strings or Frogs both at once.
Generic types address this problem.  Now you can say that your List is only for Strings like this:
What gets tricky is when you use wildcards.  For generics, "?" is the character that indicates a wildcard.
This does not mean "a List of anything", but "a List of some type which I haven't told you about yet."

Take a look at this for more information: https://docs.oracle.com/javase/tutorial/java/generics/index.html
 
Abhay Bhatt
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:

Second, the type being referred to here is the generic, and not the type of the object... but yes, you are declaring a reference to a Class instance, whose generic can be any type

Henry


I think may be I am going a bit too out of focus here, when its not necessary. Can you please tell me if this 'generic type' thing you mentioned above is applicable for and ? Because I was only concerned with these two originally.
 
Knute Snortum
Sheriff
Posts: 4273
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, let's look at this snippet:

If you just write Class clazz you get a compiler warning, because the compiler doesn't know what type (generic type) of Class you mean.  If you want to capture an instance of Class in a variable, you can use Class<ClassPlay> clazz.  Now the compiler knows that only a class of ClassPlay can be used in the variable clazz.

But what if you don't know what the class is going to be named?  Then you can use Class<?> clazz.  This is a generic wildcard.  It says, "I am a variable that holds a reference to Class that can only be one type, but I haven't told you what the type is yet."

I'm putting all this is colloquial terms which are less than accurate so that you can get the feeling of what's going on.
 
Abhay Bhatt
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
If you just write Class clazz you get a compiler warning, because the compiler doesn't know what type (generic type) of Class you mean. 


Thanks a lot! I really appreciate, this explanation is the most lucid/clear/good I've encountered to a question which I asked, since a long time!

Regarding above quoted statement, doesn't compiler know already what the 'type' is, since 'Class' is a class and therefore a 'type'? Why is this need to know this 'generic type' w.r.t. the 'Class'?
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Class type is generic because doing that makes various reflection processes be more type-safe.

Generally making something generic is to make some associated processing type-safe, i.e. you don't have to write casts all over the place. In this case the "some associated processing" is reflection.

Notice also the cast(Object) method of Class<T>; it allows you to assign a reference to an Object of unknown type to a variable of type T. It will still throw a ClassCastException if that assignment isn't possible, you can't avoid that, but it's another way of casting. Presumably this would be something you could use while writing reflection code.
 
Knute Snortum
Sheriff
Posts: 4273
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhay Bhatt wrote:doesn't compiler know already what the 'type' is, since 'Class' is a class and therefore a 'type'? Why is this need to know this 'generic type' w.r.t. the 'Class'?

The compiler knows that the type is Class, but it doesn't know what type of class can be instantiated.  For instance:
What type of class does clazz hold?  The compiler doesn't know.  But do this:
Now you get a compiler error if you try to set clazz to anything other than a Foo.class (or some class that can be converted to Foo).  This is called type safety.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!