How to determine an object's class?

后端 未结 11 999
醉酒成梦
醉酒成梦 2020-11-22 17:10

If class B and class C extend class A and I have an object of type B or C, how can I determine of which type

相关标签:
11条回答
  • 2020-11-22 17:43

    Multiple right answers were presented, but there are still more methods: Class.isAssignableFrom() and simply attempting to cast the object (which might throw a ClassCastException).

    Possible ways summarized

    Let's summarize the possible ways to test if an object obj is an instance of type C:

    // Method #1
    if (obj instanceof C)
        ;
    
    // Method #2
    if (C.class.isInstance(obj))
        ;
    
    // Method #3
    if (C.class.isAssignableFrom(obj.getClass()))
        ;
    
    // Method #4
    try {
        C c = (C) obj;
        // No exception: obj is of type C or IT MIGHT BE NULL!
    } catch (ClassCastException e) {
    }
    
    // Method #5
    try {
        C c = C.class.cast(obj);
        // No exception: obj is of type C or IT MIGHT BE NULL!
    } catch (ClassCastException e) {
    }
    

    Differences in null handling

    There is a difference in null handling though:

    • In the first 2 methods expressions evaluate to false if obj is null (null is not instance of anything).
    • The 3rd method would throw a NullPointerException obviously.
    • The 4th and 5th methods on the contrary accept null because null can be cast to any type!

    To remember: null is not an instance of any type but it can be cast to any type.

    Notes

    • Class.getName() should not be used to perform an "is-instance-of" test becase if the object is not of type C but a subclass of it, it may have a completely different name and package (therefore class names will obviously not match) but it is still of type C.
    • For the same inheritance reason Class.isAssignableFrom() is not symmetric:
      obj.getClass().isAssignableFrom(C.class) would return false if the type of obj is a subclass of C.
    0 讨论(0)
  • 2020-11-22 17:43

    Any use of any of the methods suggested is considered a code smell which is based in a bad OO design.

    If your design is good, you should not find yourself needing to use getClass() or instanceof.

    Any of the suggested methods will do, but just something to keep in mind, design-wise.

    0 讨论(0)
  • 2020-11-22 17:46

    checking with isinstance() would not be enough if you want to know in run time. use:

    if(someObject.getClass().equals(C.class){
        // do something
    }
    
    0 讨论(0)
  • 2020-11-22 17:47

    We can use reflection in this case

    objectName.getClass().getName();
    

    Example:-

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        String name = request.getClass().getName();
    }

    In this case you will get name of the class which object pass to HttpServletRequest interface refference variable.

    0 讨论(0)
  • 2020-11-22 17:48

    You can use:

    Object instance = new SomeClass();
    instance.getClass().getName(); //will return the name (as String) (== "SomeClass")
    instance.getClass(); //will return the SomeClass' Class object
    

    HTH. But I think most of the time it is no good practice to use that for control flow or something similar...

    0 讨论(0)
提交回复
热议问题