Note: I am not asking what types can be used to store a class name nor I am asking for work-arounds to store class names such as reflection or using String
s. This is also not a question on asking how to use instanceof
operator.
Consider this example:
A a = new A();
Class c1 = A.class;
System.out.println(a instanceof c1); //Does not Work
Class c2 = A.class.getClass();
System.out.println(a instanceof c2); //Does not Work
class A{}
I know the above codes will fail. But I am curious whether it is possible to store a class such that it can work as though I am typing out the class name itself.
System.out.println(a instanceof A); //This will work
Can we hold (class)A
with some variables xx
so that this will work?
System.out.println(a instanceof xx);
My question is: Is it possible to store A
with a variable such that the compiler will treat that variable as though it is seeing A
?
Note: I am not asking how to store object of A. I am asking is it possible to store A itself.