Suppose I have the following classes:
class A {}
class B extends A {}
A instA = new A();
A instB = new B();
Now as far Java is concerned instB is of type A.
How can I find out what is instB’s actual type?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use
instB.getClass(). This will return aClassobject representinginstB‘s runtime type.Source: javadoc
You can also use the following conditional check with
instanceof:instanceofwould be the more conventional way, and the conditional will also return true ifinstBis a subclass ofB.You may want to further explain your use case, with code examples, to get better discussion.