How to get the type of the class for comparison
-
19-09-2019 - |
Question
I have this object which is an instance of a superclass. I want to know which subclass that object really is, so that I can decide what to do with it. There is this getClass() method but it's apparently not used for comparison issues. How can I get the sub-type of my object?
Solution
You may have a design flaw if you're trying to do this but instanceof
.
public class MainClass {
public static void main(String[] a) {
String s = "Hello";
if (s instanceof java.lang.String) {
System.out.println("is a String");
}
}
}
OTHER TIPS
Class c = (your super class name).getClass();
if(c.getName == "your sub class name") take action
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow