Getters and setters are fundamental concepts in Java, used to encapsulate fields or instance variables. They are accessor and mutator methods that control how the exterior code interacts with object fields. Let's explore what these methods are and understand their role in Object-Oriented Programming (OOP) with examples.

In Java, we can't directly access or change the values of instance variables or fields. To do so, we define public methods known as getters and setters. Getters are used to access or 'get' the values of the field, while setters are used to modify or 'set' the values of the field. This way, we ensure the encapsulation principle of OOP, making our code secure and robust.

Getters and Setters
As mentioned, getters and setters are methods that provide access to, and manipulate, the values of instance variables. They are declared in the class and usually prefixed with 'get' and 'set', respectively. Let's dive into the details and understand how to use them with examples.

Getters (Accessor Method)
A getter method is used to read or retrieve the value of an instance variable. It's named starting with "get" followed by the field name in camelCase. For example, for a field "name", the getter method would be named "getName". Here's a simple Java class to illustrate this:

public class User {
private String name;
public String getName() {
return name;
}
}Here, we have encapsulated the 'name' field as private, and provided a public getter method to access its value. You can access 'name' with "user.getName()".
Setters (Mutator Method)

Now, let's look at the setter method. A setter is used to change the state of an object by modifying its field's value. It's named starting with "set" followed by the field name. For instance, a setter for the 'name' field would be named "setName". Here's how you might implement it:
public class User {
private String name;
public void setName(String name) {
this.name = name;
}
}Here, 'setName' is a setter method that allows you to set the 'name' field. You'd use it like this: "user.setName("John Doe")"

Shorter Syntax with Java Bean Convention
Since Java 8, you can also use the shorter syntax for getters and setters using Java's 'Bean' convention. It automatically generates the methods for you, resulting in more concise code.



















Automatic Getter Generation
To generate a getter for a variable, simply use the getter method of the class, passing the variableβs name as an argument. Here's an example:
public class User {
private String name;
public String get(String name) {
return this.name;
}
}
This will generate the getName method automatically.
Automatic Setter Generation
For setters, use the setter method of the class, passing the variableβs name and a value as arguments. Here is how you would generate a setter:
public class User {
private String name;
public void set(String name, String value) {
this.name = value;
}
}
This will generate the setName method, with the given value to set the 'name' field.
The final part you should take away is that getters and setters are not just handy syntax in Java, but they form the basis of the principle of encapsulation - one of the fundamental principles of Object-Oriented Programming (OOP). They ensure our data is safe and usage is controlled. Happy coding!