Getters and setters for every field, or even just getters for immutable ones. When inheritance is intended I understand the rationale but for serializable data classes, publically accessible fields should be made public.
Most of the time a final field with a getter can be made public, the only reason not to is to avoid using generics (by taking advantage of covariant return types)
I am a software development engineer in test for Infosys. My job is officially to write automated tests in Selenium Webdriver. I'm also a web developer as a hobbyest
Why not just make the field public? What's the point in making getters and setters and not just dealing with the field directly? I asked one instructor about this and he just said "it's better encapsulation" but I don't see it.
Getters and setters for every field, or even just getters for immutable ones. When inheritance is intended I understand the rationale but for serializable data classes, publically accessible fields should be made public.
Most of the time a final field with a getter can be made public, the only reason not to is to avoid using generics (by taking advantage of covariant return types)
I've never fully understood getters and setters. If I have a field that has both a getter and a setter like so:
Why not just make the field public? What's the point in making getters and setters and not just dealing with the field directly? I asked one instructor about this and he just said "it's better encapsulation" but I don't see it.
It allows you to validate and know when fields are accessed.
Getters and Setters allow you to validate and normalize input. If this validation isn't needed then consider a public field.