How can I access the value of a private attribute in a class, from a class in another package, without using an access method, such as a getter?
class Person{
private String name = "Someone";
}
Why would anyone do this?
One of the reasons is that you may need to serialize objects, such as Jackson does, transforming the values of the class fields into JSON . I can access using field getters, but if I do not have them, or they do not follow the JavaBeans pattern, in the example getName
, how do I access the value of the field directly. Jackson itself allows you to configure whether you want to access through setters and getters, or with the fields directly. There is a real performance gain.