I've been looking at a better understanding of the set/get methods. So far I have come up with the following. Could anyone confirm whether my understanding is correct and if not elaborate more.
accessor methods Normally you don't make variables in your class public. Instead you provide public set and get methods for the variable. This lets you then later change the way the variable is stored, or add extra validation checks.
Method names made up of a member name with a get or set prefix are methods that are required because the members are private and therefore cannot be accessed from outside the class. Any program that creates instances of a class will use these methods to access the members. A member does not need a set method when its value is changed in other methods.
The get/set methods are not compulsory, only include them for members when other classes require access to them.
The get methods (also called an accessor method) return the value of the member they are associated with and the set method (also called a mutator method) sets the member to a new value.
Using get and set methods aids maintainability, flexibility and extensibility. Good design includes encapsulation � to do this you make public accessor methods and force calling code to use those methods. i.e. set and get
Yep, Maureen, you sounds like you have a good understanding of the getters & setters.
If you want your POJOs to be readonly, do not provide any setters. Similarly for variables that you do not wish to be modified by others directly, do not provide a setter for it. As you had mentioned, this variable's value could be computed from other values, & by not providing a setter, you don't risk it being changed by others.
As for the validation checking, this is usually not done inside the getter/setter methods. You could do these validation checkings before calling these methods.