Kousha Kousha -4 years ago 157
Java Question

Java pass a class to check instance of

I want to pass a class to a function. In that function, new instance of other classes are created.

Then, I want to be able to find which object there is an instance of the class I passed:

public void doSomething(Class cls) {
SomeObject obj = new SomeObject();

if (obj instanceof cls) {
// Do amazing things here
}
}

class Person {
// This exists
}

doSomething(Person.class);


The code above doesn't work. I hope I'm clear enough what I'm trying to do.

Answer Source
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download