0

I have a type of Object variable called obj (Object obj). I'm using obj.getClass() and I want to check if the returned class is actually a certain class, in my case a class named Student. How do I do that?

I tried obj.getClass().isInstance(Student) but it tells me that Student cannot be resolved to a variable. Also yes I've done my research and have found similar questions on SO but for some reason nothing I've done works.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
JohnEm
  • 29
  • 5

1 Answers1

1

You can use ‘Student.class.isAssignableFrom(obj.getClass())‘ or if you have the instance of the object just ‘obj instanceof Student‘.

Nightloewe
  • 918
  • 1
  • 14
  • 24