Lesson 7 | Getter and Setter methods |
Objective | Learn about Getter and Setter methods. |
JavaBeans Getter and Setter Methods
In the context of JavaBeans, which are reusable software components for Java conforming to a specific convention, getter and setter methods play a fundamental role in encapsulating access to the properties of a bean. JavaBeans utilize these methods to adhere to the principle of encapsulation, a core concept in object-oriented programming that restricts direct access to some of an object's components.
Getter Methods:
Getter methods are used to retrieve or access the value of a bean's property. They follow a naming convention that starts with "get" followed by the name of the property with its first letter capitalized. In the case of a boolean property, the getter may alternatively start with "is". These methods do not take parameters and return a value corresponding to the property's data type. The primary purpose of a getter method is to provide read access to a property while keeping the property itself encapsulated and protected from direct external access.
Example:
For a property named `color`, the getter method would be defined as:
public String getColor() {
return this.color;
}
For a boolean property `visible`, the getter method could be:
public boolean isVisible() {
return this.visible;
}
Setter Methods: Setter methods are used to set or update the value of a bean's property. These methods follow a naming convention similar to getter methods, starting with "set" followed by the property name with its first letter capitalized. Setter methods take exactly one parameter, matching the property's data type, and typically return void. They provide a controlled way to modify the value of a property, allowing for validation, transformation, or notification mechanisms to be implemented within the setter method before the property value is actually updated.
Example:
For the `color` property, the setter method would be defined as:
public void setColor(String color) {
this.color = color;
// Additional validation or operations can be performed here
}
Purpose and Usage:
Getter and setter methods are essential for:
- Encapsulation: They encapsulate the properties, ensuring that external classes access and modify the properties only through these methods. This abstraction layer allows the internal representation to be changed without affecting external classes that rely on the JavaBean.
- Flexibility: They provide the flexibility to add logic, such as validation or event firing, when a property is accessed or modified, without requiring changes to the calling code.
- Compatibility: They ensure compatibility with various Java frameworks and tools that rely on the JavaBeans convention for introspection, serialization, and property manipulation.
By adhering to the JavaBeans standard and using getter and setter methods, developers ensure that their Java components are easily reusable, interoperable, and maintainable within the vast ecosystem of Java development.
Accessor methods responsible for reading a the properties of a JavaBean are known as getter methods,
while those responsible for writing a Bean's properties are known as setter methods. Getter and setter methods form the sole interface between the properties of a Bean and the outside world, which is due to the fact that properties can not be publicly accessed directly. You must always work with properties through getter and setter methods.
Getter method: an accessor method that reads, or gets, the value of a property.
Setter method: an accessor method that writes, or sets, the value of a property.