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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With