Using Accessors and Mutators in Java

Woman Writing Code on Laptop

Paul Leahy is a computer programmer with over a decade of experience working in the IT industry, as both an in-house and vendor-based developer.

Updated on January 22, 2019

One of the ways we can enforce data encapsulation is through the use of accessors and mutators. The role of accessors and mutators are to return and set the values of an object's state. Let's learn how to program accessors and mutators in Java. As an example, we'll use a Person class with the state and constructor already defined:

Accessor Methods

An 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. For example let's add accessor methods for firstname, middleNames and lastname:

These methods always return the same data type as their corresponding private field (e.g., String) and then simply return the value of that private field.

We can now access their values through the methods of a Person object:

Mutator Methods

A mutator method is used to set a value of a private field. It follows a naming scheme prefixing the word "set" to the start of the method name. For example, let's add mutator fields for address and username:

These methods do not have a return type and accept a parameter that is the same data type as their corresponding private field. The parameter is then used to set the value of that private field.

It's now possible to modify the values for the address and username inside the Person object:

Why Use Accessors and Mutators?

It's easy to come to the conclusion that we could just change the private fields of the class definition to be public and achieve the same results. It's important to remember that we want to hide the data of the object as much as possible. The extra buffer provided by these methods allows us to:

Let's say we decide to modify how we store middle names. Instead of just one String we can now use an array of Strings:

The implementation inside the object has changed but the outside world is not affected. The way the methods are called remains exactly the same:

Or, let's say the application that is using the Person object can only accept usernames that have a maximum of ten characters. We can add validation in the setUsername mutator to make sure the username conforms to this requirement:

Now if the username passed to the setUsername mutator is longer than ten characters it is automatically truncated.

Cite this Article Your Citation

Leahy, Paul. "Using Accessors and Mutators in Java." ThoughtCo, Jun. 25, 2024, thoughtco.com/accessors-and-mutators-2034335. Leahy, Paul. (2024, June 25). Using Accessors and Mutators in Java. Retrieved from https://www.thoughtco.com/accessors-and-mutators-2034335 Leahy, Paul. "Using Accessors and Mutators in Java." ThoughtCo. https://www.thoughtco.com/accessors-and-mutators-2034335 (accessed September 5, 2024).

copy citation Beginner's Guide to Using an IDE Versus a Text Editor What Is Java? Java Is Case Sensitive Understanding Java's Cannot Find Symbol Error Message Reserved Words in Java Using Java Comments Using Multiple Main Classes Disabling (or Enabling) the Java Plugin in a Browser Create a Simple Window Using JFrame Developing a Java GUI The Java Constructor Method Common Java Runtime Errors How to Use a Constant in Java Understanding the Concatenation of Strings in Java Coding a Simple Java User Interface Using NetBeans and Swing Message Dialog Box Java Program ThoughtCo is part of the Dotdash Meredith publishing family.

We Care About Your Privacy

We and our 100 partners store and/or access information on a device, such as unique IDs in cookies to process personal data. You may accept or manage your choices by clicking below, including your right to object where legitimate interest is used, or at any time in the privacy policy page. These choices will be signaled to our partners and will not affect browsing data.

We and our partners process data to provide:

Store and/or access information on a device. Use limited data to select advertising. Create profiles for personalised advertising. Use profiles to select personalised advertising. Create profiles to personalise content. Use profiles to select personalised content. Measure advertising performance. Measure content performance. Understand audiences through statistics or combinations of data from different sources. Develop and improve services. Use limited data to select content. List of Partners (vendors)