Javatpoint Logo
Javatpoint Logo

Accessor and Mutator in Java

In object-oriented programming, encapsulation is a fundamental concept that refers to the practice of hiding the implementation details of an object and providing an interface to access its properties and behaviors. Accessor and mutator methods are two important concepts related to encapsulation in Java.

Accessor

Accessor methods, also known as getter methods, are methods that allow you to retrieve the value of an object's private instance variables. These methods provide read-only access to the object's state. By using accessor methods, you can ensure that the object's state is not modified accidentally or maliciously by external code.

Mutator

Mutator methods, also known as setter methods, are methods that allow you to modify the value of an object's private instance variables. These methods provide write-only access to the object's state. By using mutator methods, you can ensure that the object's state is modified only through a controlled interface.

Let's take a look at an example to understand the concept of accessor and mutator methods in Java. Suppose we have a class called Person that has three private instance variables: name, age, and email. We want to provide access to these variables using accessor and mutator methods.

In this example, we have defined three accessor methods: getName(), getAge(), and getEmail(), and three mutator methods: setName(), setAge(), and setEmail(). The accessor methods return the value of the corresponding instance variable, while the mutator methods set the value of the corresponding instance variable.

Naming Convention

The naming convention for accessor and mutator methods is important in Java. Accessor methods should be named starting with "get" followed by the name of the variable, with the first letter capitalized. Mutator methods should be named starting with "set" followed by the name of the variable, with the first letter capitalized. This naming convention makes it clear what each method does and makes the code more readable and maintainable.

Accessor and mutator methods are an essential part of encapsulation in Java. They allow you to control access to an object's state, ensuring that it is accessed and modified only through a controlled interface. By following the naming convention for accessor and mutator methods, we can make your code more readable and maintainable.

here's an example program that demonstrates the use of accessor and mutator methods in Java:

Person.java

Output:

Name: Manoj
Age: 21
Email: [email protected]

In this example, the main method creates a new instance of the Person class and uses the mutator methods (setName, setAge, and setEmail) to set the values of the object's private instance variables. Then, the accessor methods (getName, getAge, and getEmail) are used to retrieve the values of the instance variables and display them on the console.

Advantage of Using Accessor and Mutator

Advantage of using accessor and mutator methods is that they allows us to add validation and other processing logic to the process of getting and setting an object's state. For example, you could add a validation check to the "setAge(int age)" method to ensure that the age value is within a certain range, or add formatting logic to the "getName()" method to capitalize the first letter of the person's name.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA