Java – it is necessary to have getters and setters in POJO

See English answers > are getters and setters poor design? 17 conventional advice seen

Solution

I don't think so. It depends on your object's life cycle and its "exposure" (external modification)

If you only use it as a data structure, exposing the field in a safe way (final) sounds enough:

public class Person {
    public final String firstName;
    public final String lastName;

    public Person(String firstName,String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>