What are accessor and mutator methods in Java?

Accessors and mutators are used in Java to get and set the value of private fields, respectively. Accessors and mutators are both referred to as getters and setters, respectively. The necessity for getter and setter methods arises because if we had designated the variables as private, only we would have access to them.

External clients should be able to utilize our classes, and create procedures for every private data element. In order to work on our private data, we must declare public methods.

Some well-known IDEs, like Netbeans, Eclipse, IntelliJ, and others, automatically build accessor and mutator methods. Let’s discuss them in detail.

Accessor

The Accessor method returns one of the object's properties. These are acknowledged as public and are accessible easily. Accessors adhere to a naming convention; in other words or terms, they append a word to the procedure name's beginning. Common terms for an accessor method include "get method" and "getter." These are employed in order to retrieve the values of a private field. Depending on their private field, these methods return the same data type.

The word "access," which enables the user to access the secret data in a class, serves as the inspiration for the name of the accessor function.

Accessors are the same as "getters," the "get" method, and other similar terms. The private constants and variables are retrieved by the getters so that they can be used outside of a class.

It is customary to write the accessor solution's name in the get word first. So that they may be seen in the class at easy accessor method, declarations must be public. However, accessor methods don't take any arguments. But they give the value of a private variable back. As a result, it calls the function and returns the value from the private variable. In order to return private variables, we typically need to write many accessor methods in the class, each with a unique name.

Syntax used for accessor:

In programming languages like Java, accessors are denoted by the term "get." We can use the getter getName()  to get the variable  "name". Take a look at the example of the accessor method below.

Syntax:

public String getmethodname ()
{
	return value;
}

Another Syntax:

public int getValue()
{
	return Value;
}

Example:

public class Employee
{
	private String data;
	public String getName()
	{
		return data;
	}
} 

Note: Please note that each getter contains the term "get" in the method Employee information data before the variable name, and the return type matches the type of the variable being returned. The getter/accessor method also returns a "String" because the variable "data" is of the "String" type.

Example for accessor method 1:

public class Student 
{  
    private int name;  
    public int getName() 
    {  
        return name;  
    }  
    public void setName(int newName) 
    {  
        name = newName;  
    }  
}  	  

Example 2:

//a program illustrating th java accessor method "getter" method
public class Employee 
{
    // variables of student class using the private 
    private int eid;
    private String ename;


    // using the constructors
    Employee(int e, String en) 
    {
        this.eid = e;
        this.ename = en;
    }


    // to get the eid and ename using the accessor method
    public int getEid() 
    {
        return eid;
    }
    public String getEname() {
        return ename;
    }


    // the main method of the java programming language 
    public static void main(String s[]) 
    {
        Employee emp = new Employee(15, "chandu varada");


        // the calling method for accessor method 
        System.out.println("Employee id  - " + emp.getEid());
        System.out.println("Employee Name  - " + emp.getEname());
    }
} 

Output:

Employee id  - 15
Employee Name  - chandu varada

Mutator

A procedure called a "mutator" modifies or "mutates" something.

It demonstrates the idea of encapsulation. A set method, often known as a setter, is another name for a Mutator method. They go by the name of modifiers as well. These are simple to identify because they begin with the word set. They have been deemed public. In accordance with their private field, mutator methods either accept a parameter of the same data type or do not have a return type at all. The value of the private field is then set using it.

We use the Mutator method to modify an object's characteristics in Java. In other words, the Mutator method sets the initial value of a class's instance variable or a private class variable. We can therefore conclude that encapsulation is provided via the mutator approach.

Generally, the set word is written at the start of the mutator method's name; to make sure they stand out in the class. Public methods for mutators must be declared. However, there is no return type for Mutator methods. But based on the type of the private variable, they may accept a parameter. Then, it will use the keyword this to access the private variable and set its value to the parameter's value. In the class, we can create multiple mutator methods with unique names.

Since private data members cannot be directly updated, setters help with encapsulation. The value of a variable can therefore be changed outside the scope of a class using setter methods or mutators.

The class instance variable's value is stored or modified using the Mutator method.

The syntax for the Mutator method:

public void set Size(int Size) 
{  
    this.Size = Size;  
}  

Syntax 2:

public void setmethodname(element1, element2, ...) 
{
   //private variables of the elements are going to be set in this method
}

Syntax 3:

public class Employee 
{
private String phno;
public void setPhno(String phno)
 {
		this.phno = phno;
	}
}

Example for mutator method:

import java.util.*;
//a program demonstrating the mutator method
public class Employee
{
    // private variables of an employee class are to be declared
    private int eid;
    private String ename;


    // the multiple and many details of the mutator method to be set by using this method
    public void setDetails(int e, String en) {
        System.out.println("Setter method inside");
        // to access the class variables, we have to use this method
        this.eid = e;
        this.ename = en;
    }
    public static void main(String s[]) 
    {
        Employee emp = new Employee();
        // we have to call the mutator method in the main method
        emp.setDetails(15, "chandu varada");
    }
}

Output:

public int getValue()
{
	return Value;
}
0

Examples for both accessor and mutator methods:

public int getValue()
{
	return Value;
}
1

Output:

public int getValue()
{
	return Value;
}
2

Difference between Accessor and Mutator

  • The accessor method will return a value, whereas the mutator method will not return any values.
  • The getPriority() method can be used by the accessor method, whereas the setPriority() method can be used by the mutator method.
  • The Java.lang package's Thread class contains this method in both the setter and getter methods.
  • We give the thread name in a setter and get it back in a getter.

Conclusion

The main aim of the accessor and mutator are:

These techniques stop unauthorized access to these items, whether purposeful or accidental.

Our major goal is to conceal the data of the object as much as we can. These techniques also enforce validation on the values that are being set.

What is mutator methods in Java?

In Java, mutator methods reset the value of a private variable. This gives other classes the ability to modify the value stored in that variable without having direct access to the variable itself. Mutator methods take one parameter whose type matches the type of the variable it is modifying.

What is the difference between accessor and mutator methods?

An accessor is a class method used to read data members, while a mutator is a class method used to change data members. It's best practice to make data members private (as in the example above) and only access them via accessors and mutators.

Which methods are mutators?

In computer science, a mutator method is a method used to control changes to a variable. They are also widely known as setter methods. Often a setter is accompanied by a getter (together also known as accessors), which returns the value of the private member variable.

What is constructor methods and accessor methods?

Constructor is a block of code, which runs when you use new keyword in order to instantiate an object. http://beginnersbook.com/2013/03/constructors-in-java/ A accessor method is used to return the value of a private field. It follows a naming scheme prefixing the word "get" to the start of the method name.