Does anyone know a better way to write the following statement:
example.mySpecialMethod(new MySpecialClass[0].getClass())
I need the array type, but I dont know if there is a better solution. At the moment this example works for me, but perhaps someone knows a better way to do the same without the new
keyword.
Well, you can get the element type of the array: Type type = array. GetType(). GetElementType();
What is the class name of Java array? In Java, an array is an object. For array object, a proxy class is created whose name can be obtained by getClass().getName() method on the object.
get() is an inbuilt method in Java and is used to return the element at a given index from the specified Array. Parameters : This method accepts two mandatory parameters: array: The object array whose index is to be returned.
An Array of Objects is created using the Object class, and we know Object class is the root class of all Classes. We use the Class_Name followed by a square bracket [] then object reference name to create an Array of Objects.
The class of new MySpecialClass[0]
is MySpecialClass[].class
so you can use:
example.mySpecialMethod(MySpecialClass[].class)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With