How to read the value of a private field from a different class in Java?

前端 未结 14 1691
梦毁少年i
梦毁少年i 2020-11-21 11:28

I have a poorly designed class in a 3rd-party JAR and I need to access one of its private fields. For example, why should I need to choose priv

14条回答
  •  太阳男子
    2020-11-21 11:50

    As oxbow_lakes mentions, you can use reflection to get around the access restrictions (assuming your SecurityManager will let you).

    That said, if this class is so badly designed that it makes you resort to such hackery, maybe you should look for an alternative. Sure this little hack might be saving you a few hours now, but how much will it cost you down the road?

提交回复
热议问题