12

I am writing a method were I would like to pass a class to a method, where a part of the code includes checking if the object is of a certain type. This is what I want (but which obviously doesn't work):

private static class MyClass1 { /***/ }
private static class MyClass2 { /***/ }

private void someFunc() {
    /* some code */
    methodName(MyClass1);
    methodName(MyClass2);
}


private void methodName(Class myClass) {
    Object obj;
    /* Complicated code to find obj in datastructure */
    if (obj instanceof myClass) {
        /* Do stuff */
    }
}

Any hints as to how this can be done? Thanks!

tor
  • 636
  • 3
  • 7
  • 18

3 Answers3

23

Class has both an isInstance() method and an isAssignableFrom() method for checking stuff like that. The closest to what you're looking for is:

if (myClass.isInstance(obj)) {

Update: From your comment, you want to pass the name of a class into a method and check if something is assignable to that class. The only way to do that is to pass the class name as a String, then load the class and use one of the aforementioned methods. For instance (exception handling omitted):

private void methodName(String className) {
    Class myClass = Class.forName(className);
    Object obj;
    /* Complicated code to find obj in datastructure */
    if (myClass.isInstance(obj)) {
        /* Do stuff */
    }
}
Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • I don't believe I formulated my question clear enough - the trouble is that I want to be able to use instanceof, i.e. I want the "Class" I am passing to be the name of a Class. – tor Feb 13 '13 at 05:23
  • 1
    Updated. Is that closer to what you're looking for? – Ryan Stewart Feb 13 '13 at 05:26
3

I think you want to know object's class type at runtime.. so use reflaction api for that. and for your problem this solution i think work

public class Clazz {
public static void main(String[] args) {
    Clazz clazz = new Clazz();
    ArrayList list = new ArrayList<>();
    Class myClass = list.getClass();
    clazz.display(myClass);
}

/**
 * Modified By nirav.modi on Feb 13, 2013
 */
private void display(Class myClass) {
    ArrayList list = new ArrayList<>();
    if(myClass.isInstance(list)) {
        System.out.println("Yooo , its instance..");
    }else {
        System.out.println("Not instance");
    }
}

}

NIrav Modi
  • 6,038
  • 8
  • 32
  • 47
0
MyClass1 myClass1 = new MyClass1();
if(MyClass1.class.isInstance(myClass1))
    System.out.println("(MyClass1.class.isInstance(myClass1) is TRUE");
else
    System.out.println("(MyClass1.class.isInstance(myClass1) is FALSE");

Object myClass2 = new MyClass2();
Class class1 = myClass1.getClass(); 
Class class2 = myClass2.getClass();
System.out.println("class1 == class2 : " + ( class1 == class2));
System.out.println("class1.isAssignableFrom(class2) = " 
    + class1.isAssignableFrom(class2));

MyClass1 myClass3 = new MyClass1();
Class class3 = myClass3.getClass();
System.out.println("class1 == class3 : " + ( class1 == class3));
System.out.println("class1.isAssignableFrom(class3) = " 
    + class1.isAssignableFrom(class3));

OUPUT:

(MyClass1.class.isInstance(myClass1) is TRUE
class1 == class2 : false
class1.isAssignableFrom(class2) = false
class1 == class3 : true
class1.isAssignableFrom(class3) = true
Sunil Gulabani
  • 878
  • 1
  • 8
  • 21