Java Reflection: Getting fields and methods in declaration order

前端 未结 6 958
不知归路
不知归路 2020-11-29 07:27

Is there any way to get a classes declared fields (and methods) in the order of declaration using reflection? According to the documentation, the ordering of Methods and Fie

6条回答
  •  执笔经年
    2020-11-29 07:45

    With jdk 6, the reflected fields are in deed in their declaration order. In early jdk that wasn't the case. Apparently enough people have nagged.

    Although not guaranteed by javadoc, I would still take this order as granted, and I assume the order will be kept in future jdks too.

    In your app, like in most apps, the dependency on the declaration order is mostly vanity - your app won't fail if the order screws up, it just become a little uglier.

提交回复
热议问题