0

Suppose we have a class Class<E> elementClass and we want to obtain the class of an array which would consist of elements of class elementClass. For years, java did not offer any elegant way of accomplishing this, so we have been instantiating a throwaway array just to get its class, like this:

Class<?> arrayClass = Array.newInstance( elementClass, 0 ).getClass();

So, has anything improved in this regard in java during the last three years? Do we have any decent way of obtaining the class of an array given the class of the element?

3
  • I'm not sure but I don't think if something have changed regarding that, even in Java 8 it's the same as Java 6 Commented Feb 14, 2015 at 21:52
  • Creating a zero-length array is very cheap. What's the problem here? Is this an action you'll be doing 1000's of times per second? Commented Feb 15, 2015 at 22:21
  • 1
    @Bohemian to me, it is not an issue of performance, it is an issue of elegance. Commented Feb 15, 2015 at 22:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.