I'm wrapping an Axis web service in a Springboot microservice and stumbled on some code that does not make sense.
Class cls = AmountInfo[].class;
What class is returned above ?
Here's what the docs of java.lang.Class
have to say about this:
Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions.
Just as the a Class
object for the type AmountInfo
, there is a Class
for the array 1D array type AmountInfo[]
, just as there is also a type for the 2D array type AmountInfo[][]
, etc.
A Class
of an array type returns true
when isArray()
is invoked on it. AmountInfo[].class
is equivalent to AmountInfo.class.arrayType()
. And you can use it with the reflection API the same way you can use a normal class (example below):
//Creating a 1D array of AmountInfo type
jshell> Array.newInstance(AmountInfo.class, 2)
$65 ==> AmountInfo[2] { null, null }
// Creating a 2D array of AmountInfo type
jshell> Array.newInstance(AmountInfo[].class, 2)
$66 ==> AmountInfo[2][] { null, null }