CodeGym /Java Blog /Methods in Java /Accessors And Mutators In Java
Author
Pavlo Plynko
Java Developer at CodeGym

Accessors And Mutators In Java

Published in the Methods in Java group
Before we dive into this concept, you need to be aware of classes and encapsulation in Java.

Encapsulation in Java

Encapsulation, as the name suggests, is the process of enclosing data and methods as a single unit. In object-oriented programming, the data members of a class are made private to restrict direct access to them. So the encapsulated data members can not be retrieved or modified without a properly-defined way. For this, we define accessor and mutator methods in Java. Let’s have a look at these methods in detail.

What are accessors and mutators in Java?

Accessors

The accessor method’s name is driven by the word “access” which allows the user to access the private information in a class. If you have ever heard of the “get” method or “getters”, it is the same thing as accessors. The getters retrieve the private variables and constants to access outside the scope of a class.

Syntax

We use the keyword “get” for Java accessors. To access the variable “name” we can use the following getter getName(). For the accessor method’s example, have a look at the following.

public class Student {

	private String name;
	
	public String getName() {
		return name;
	}
}
Kindly note that each getter has the keyword “get” before the variable name in the method signature and the return type is the same as that of the variable to be returned. Since the variable “name” is of a “String” type, so the getter/accessor method also returns a “String”.

Mutators

The mutator method in Java is driven by the word “mutate”, which literally means to modify. Mutators allow the users to set/mutate the value of private variables of a class object. In the object-oriented programming context, the “set” method or “setters” are also known as mutators. Setters facilitate encapsulation as private data members can not be modified directly. So setter methods/mutators are used to update a variable’s value outside the class scope.

Syntax

For mutators, we use the “set” keyword. Each setter is defined by the keyword “set” followed by the name of the variable that needs to be mutated. Here, we use the setter setName() that takes a String type variable as a parameter.

public class Student {

	private String name;

	public void setName(String name) {
		this.name = name;
	}
}

Why do we need accessors and mutators?

We need getters and setters or accessors and mutators to protect sensitive information in a class. The information is protected from Illegal use by using these standard methods. Moreover, the data set in a mutator can also be validated if it fulfils all the requirements of a program.

Accessor and Mutator Examples

By using the student class below, let’s look at the examples of the accessor and mutator methods.

Example


import java.util.Arrays;

public class Student {

	private String name;
	private Integer ID;
	private String DOB;
	private double GPA;
	private String[] courses;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getID() {
		return ID;
	}

	public void setID(Integer iD) {
		this.ID = iD;
	}

	public String getDOB() {
		return DOB;
	}

	public void setDOB(String dOB) {
		this.DOB = dOB;
	}

	public double getGPA() {
		return GPA;
	}

	public void setGPA(double gPA) {
		this.GPA = gPA;
	}

	public String[] getCourses() {
		return courses;
	}

	public void setCourses(String[] courses) {
		this.courses = courses;
	}

	public static void main(String[] args) {

		Student student1 = new Student();

		System.out.println("Student Bio [ Before using Accessors & Mutators ]");

		// calling accessor methods
		System.out.print("Name: " + student1.getName());
		System.out.print("\tID: " + student1.getID());
		System.out.print("\tGPA: " + student1.getGPA());
		System.out.print("\tDOB: " + student1.getDOB());
		System.out.println("\tCourses: " +  Arrays.toString(student1.getCourses()));

		// calling mutator methods
		student1.setName("Alex Coy");
		student1.setID(3115);
		student1.setGPA(2.79);
		student1.setDOB("08/08/1998");
		String[] courses = { "Object Oriented Programming", "Cryptography", "Photography", "Network Security" };
		student1.setCourses(courses);

		System.out.println("\nStudent Bio [ After using Mutators & Accessors ]");

		// calling accessor methods
		System.out.print("Name: " + student1.getName());
		System.out.print("\tID: " + student1.getID());
		System.out.print("\tGPA: " + student1.getGPA());
		System.out.print("\tDOB: " + student1.getDOB());
		System.out.println("\tCourses: " + Arrays.toString(student1.getCourses()));
	}
}

Output

Student Bio [ Before using Accessors & Mutators ] Name: null ID: null GPA: 0.0 DOB: null Courses: null Student Bio [ After using Mutators & Accessors ] Name: Alex Coy ID: 3115 GPA: 2.79 DOB: 08/08/1998 Courses: [Object Oriented Programming, Cryptography, Photography, Network Security]

Conclusion

This was a quick intro and example of accessors and mutators in Java. You are advised to create your own sample examples and test these methods for yourself. As a next exercise, you can find ways of adding getters and setters automatically by the IDE. Find out and let us know!
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION