Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the class associated to an array type?

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.

like image 826
user3280180 Avatar asked Oct 06 '14 13:10

user3280180


People also ask

How do you find the type of an array?

Well, you can get the element type of the array: Type type = array. GetType(). GetElementType();

Can you tell me the class name of an array in Java?

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.

How do I get an element from an array?

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.

How do you make an array from a class?

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.


1 Answers

The class of new MySpecialClass[0] is MySpecialClass[].class so you can use:

example.mySpecialMethod(MySpecialClass[].class)
like image 64
assylias Avatar answered Oct 19 '22 12:10

assylias