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)

3
  • I should have known this was a dupe. Heck, I'm not sure I haven't answered it before. (To self:) Tsk. Commented Jul 1, 2017 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 Commented Jul 7, 2017 at 13:25
  • I dont have an Instance - i have just a class. But getComponentType() does the job. Commented Jul 7, 2017 at 17:03

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.