What's the best way to get a Class object for an array type?

后端 未结 2 1683
青春惊慌失措
青春惊慌失措 2020-11-30 15:05

It\'s easy to get a class literal for a class:

String.class

But how can I get a class object for an array type?

This works, but it\

相关标签:
2条回答
  • 2020-11-30 15:30

    You can simply type

    Class<?> clazz = byte[].class;
    
    0 讨论(0)
  • 2020-11-30 15:37

    You can still use a class literal, even for an array type. This compiles just fine.

    Class<String[]> clazz = String[].class;
    Class<byte[]> clazz2 = byte[].class;
    

    Section 15.8.2 of the JLS states:

    A class literal is an expression consisting of the name of a class, interface, array, or primitive type, or the pseudo-type void, followed by a '.' and the token class.

    (bold emphasis mine)

    0 讨论(0)
提交回复
热议问题