1

I've got an array of any class and I need to get that class at runntime.

I'm thinking of something like that:

String[] stringArray = new String[10];
Class<?> clazz = stringArray.getClass();
if (String.class.isAssignableFrom(clazz)) {
    System.out.println("Won't happen");
}

The class of the array is not known at compile time.

I've found something simular but the other way here (String.class --> String[].class)

Armin
  • 351
  • 1
  • 4
  • 29
  • I should have known this was a dupe. Heck, I'm not sure I haven't answered it before. (To self:) *Tsk.* – T.J. Crowder Jul 01 '17 at 16:23
  • 1
    You still can use `array instanceof String[]` if that’s all you want to check. Otherwise, `array.getClass().getComponentType()` *should* be easy to find in the [Java documentation](https://docs.oracle.com/javase/8/docs/api/)… – Holger Jul 07 '17 at 13:25
  • I dont have an Instance - i have just a class. But `getComponentType()` does the job. – Armin Jul 07 '17 at 17:03

0 Answers0