lundi 25 juillet 2016

Can reflection change the state of an object?

I read the following article about reflection in Java:

http://ift.tt/2arCozO

In it, the author describes how to change the values of an object's fields through reflection. He explains how to do it even if the field has private access.

I while back, I read Joshua Block's book: "Effective Java". There, he says that, in order to prevent unsafe access to an object's fields, methods, etc, whenever possible, we should give fields and methods the most restrictive modifier (ie. private whenever possible, public or protected if it is part of the exposed api).

My question is the following:

Why bother designing your classes to not expose sensible information if it can be accessed through reflection anyway?

(Actually, I am asking for the piece of information that I am missing to understand this topic)





Aucun commentaire:

Enregistrer un commentaire