Getters and Setters Methods
Tuesday, December 5, 2023
In object-oriented programming in Java, getter and setter methods are essential to ensure controlled access to a class's attributes. These methods allow for obtaining and modifying attribute values, respecting encapsulation principles and providing a secure interface for interacting with objects.
Getter Method: Retrieve Values
In Java, the Getter method is a convention used to access and retrieve the value of a private attribute in a class. Private attributes are encapsulated, meaning they are not directly accessible from outside the class.
The Getter Method provides a controlled and secure mechanism to retrieve the value of these attributes.
The standard syntax for a Getter method is getAttributeName()
. For example, if you have a private attribute named name
, the Getter Method would be called getName()
.
public class Person {
private String name;
// Getter to retrieve the value of 'name'
public String getName() {
return name;
}
}
When you create instances of the Person
class, the name
attribute is not directly accessible from outside the class.
To retrieve the value of name
, you would use the getName()
method.
Example usage:
public class Main {
public static void main(String[] args) {
Person person = new Person();
// Using the Getter Method to retrieve the name
String name = person.getName();
System.out.println("Name: " + name);
}
}
Getter methods can include additional logic if necessary. For example, they could perform validations before returning the value, format the data, or any other necessary operation.
public class Person {
private int age;
// Getter Method with additional logic
public int getAge() {
if (age < 0) {
// If age is negative, return 0
return 0;
}
return age;
}
}
Setter Method: Modify Values
In Java, the Setter Method is a convention used to set or modify the value of a private attribute in a class. This method provides a controlled and secure interface to update the internal values of a class instance, contributing to the encapsulation principle in object-oriented programming.
The convention for a Setter Method is setAttributeName()
. For example, if you have a private attribute named name
, the Setter Method would be called setName(String newName)
.
public class Person {
private String name;
// Setter Method for the 'name' attribute
public void setName(String newName) {
this.name = newName;
}
}
To change the value of a private attribute, you use the Setter Method. This method takes a parameter representing the new value you want to assign to the attribute.
public class Main {
public static void main(String[] args) {
Person person = new Person();
// Using the Setter Method to change the name
person.setName("John");
}
}
Setter Methods often include validation logic to ensure the new value is acceptable before assigning it to the attribute. This validation can include checks for ranges, formats, or any other necessary conditions.
public class Student {
private int grade;
// Setter Method with validation
public void setGrade(int newGrade) {
if (newGrade >= 0 && newGrade <= 100) {
this.grade = newGrade;
} else {
System.out.println("Grade must be in the range of 0 to 100.");
}
}
}
Share it
Share it on your social media and challenge your friends to solve programming problems. Together, we can learn and grow.
Copied
The code has been successfully copied to the clipboard.