Does Class<SomeObject>
have only one instance which is SomeObject.class
?
That is to a function
void f(Class<SomeObject> arg)
is it possible to pass only SomeObject.class
otherwise compile time error?
Does Class<SomeObject>
have only one instance which is SomeObject.class
?
That is to a function
void f(Class<SomeObject> arg)
is it possible to pass only SomeObject.class
otherwise compile time error?
Yes.
As describe at the documentation:
Returns the runtime class of this Object. The returned Class object is the object that is locked by static synchronized methods of the represented class.
But for the method void f(Class<SomeObject> arg)
you can pass something as void f(Class<? extends SomeObject> arg)
. Look this question.
Passing an Class<? extends SomeObject arg
you can do something like that:
myMethod(Class<? extends BasicObject> clazz)
{
if (!clazz.isInstance(CodeObject))
{
(do something different)
}
...
}
You can always pass null
, but barring that, yes the only valid parameter would be SomeObject.class
which is loaded with the same ClassLoader
as the class that contains void f(Class<SomeObject> clazz)
.
You can have multiple distinct instances of SomeObject.class
, but they will need to be loaded by different classloaders (otherwise they will not be separate instances, but all will refer to the same Class object).
Yes
maybe You thing in this way?
Class< ? extends SomeObject>
this is notation for class of SomeObject
or overrides (something wrong with syntax highlighter, don't accept ?, space between < ? is not required)
It does seems true that each class has only one corresponding Class
instance, see this discussion
Your method could also be invoked by a raw Class
argument :) unfortunately.
Class clazz = AnotherClass.class;
f(clazz);