Accessors and Mutators in Java

Introduction

In Java accessors are used to get the value of a private field and mutators are used to set the value of a private field. Accessors are also known as getters and mutators are also known as setters. If we have declared the variables as private then they would not be accessible by all so we need to use getter and setter methods.

Accessors

An Accessor method is commonly known as a get method or simply a getter. A property of the object is returned by the accessor method. They are declared as public. A naming scheme is followed by accessors, in other words they add a word to get in the start of the method name. They are used to return the value of a private field. The same data type is returned by these methods depending on their private field.

Syntax:

public int getNumber()

{

  return Number;

}

Example:

public class Employee

{

    private int number;

    public int getNumber()

    {

        return number;

    }

 

    public void setNumber(int newNumber)

    {

        number = newNumber;

    }

}

Mutators

A Mutator method is commonly known as a set method or simply a setter. A Mutator method mutates things, in other words change things. It shows us the principle of encapsulation. They are also known as modifiers. They are easily spotted because they started with the word set. They are declared as public. Mutator methods do not have any return type and they also accept a parameter of the same data type depending on their private field. After that it is used to set the value of the private field.

Syntax:

public void set Age(int Age)

{

   this.Age = Age;

}

Example:

public class Cat

{

    private int Age;

    public int getAge()

    {

        return this.Age;

    }

    public void set Age(int Age)

    {

        this.Age = Age;

    }

}

How to Create Accessors and Mutators Automatically

Accessors and mutators can be generated automatically by the editor. Use the following procedure.

  • Move the cursor towards the end of all private fields.

    step0.jpg
     
  • Right-click and then select insert code from there.

    step1.jpg
     
  • Select getter from the list and then the window appears then check the required checkbox and then click ok.

    step2.jpg
     
  • Repeat the entire process for setters.

    step3.jpg

Purpose of Accessors and Mutators

Our main purpose is to hide the data of the object as much as we can, so these methods prevent illegal access to these objects either willful or not. Validation is also imposed by these methods on the values that are being set.

Difference Between Accessors and Mutators

  • Any value is not returned by a mutator method while the accessor method returns a value.
     
  • A Mutator method is a setPriority( ) method, on the other hand the accessor method is the getPriority method.
     
  • This method is present in both setter and getter in the Thread class in java.lang package.
     
  • In a setter we set the name to the thread and in the getter we retrieve the name to the thread.

Summary

This article introduces the accessor and mutator methods in Java, including the differences between accessors and mutators.

Up Next
    Ebook Download
    View all
    Learn
    View all