Java reflection: Is the order of class fields and methods standardized?

ivan_ivanovich_ivanoff picture ivan_ivanovich_ivanoff · Jul 8, 2009 · Viewed 31k times · Source

Using reflection on Java classes to access all field, methods, and so on:
Is there a standardized order of these elements (which is specified in some standard)?

Of course, I could check it empirically, but I need to know if it's always the same.

EDIT:
I waited for the question: What I need the order for ;)
Long story short: I have JAXB-annotated classes, and want no represent these classes visually. While the order of XML attributes is neither relevant for the XML standard, nor for JAXB, I want to have a certain order the XML attributes for the visual representation.
For example: start comes after end. This hurts one's intuition.

Answer

Adam Paynter picture Adam Paynter · Jul 8, 2009

According to the documentation:

getFields()

Returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object. The elements in the array returned are not sorted and are not in any particular order. This method returns an array of length 0 if the class or interface has no accessible public fields, or if it represents an array class, a primitive type, or void.

getMethods()

Returns an array containing Method objects reflecting all the public member methods of the class or interface represented by this Class object, including those declared by the class or interface and those inherited from superclasses and superinterfaces. Array classes return all the (public) member methods inherited from the Object class. The elements in the array returned are not sorted and are not in any particular order. This method returns an array of length 0 if this Class object represents a class or interface that has no public member methods, or if this Class object represents a primitive type or void.