Leading by example, @Joseinnewworld joined the wave and lit up the scene with 9 #NFTs sparks ⚡🔥 A true leader in the collector community — thank you for keeping the energy alive 🙌 #eCash $XEC #NFTCommunity #nftprimearts #NFTCollectors #NFTdrop #NFTartist #CryptoMarket pic.twitter.com/GrKhIlETCf
— NFToa (@nftoa_) September 3, 2025
Accessor Methods
It is a method used to return the value of a private field. This scheme is usually marked with the name prefix "get", for example, let's say we will add accessor methods for firstname, middleNames and lastname:
//Accessor for firstName
public String getFirstName()
{
return firstName;
}
//Accessor for middleNames
public String getMiddlesNames()
{
return middleNames;
}
//Accessor for lastName
public String getLastName()
{
return lastName;
}
Accessors and Mutators Java Programming
This method always returns a value with the same data type as the private field correspondence, for example (String);
public class PersonExample
{
public static void main(String[] args)
{
Person dave = new Person("Dave", "Bob Bill", "Davidson", "12 Pall Mall");
System.out.println(dave.getFirstName() + " " + dave.getMiddlesNames() + " " + dave.getLastName());
}
}Mutator Methods
It is a method used to set the value of a private field. This scheme is usually marked with the name prefix "set", for example, let's say we're going to add mutator fields for address and username:
//Mutator for address
public void setAddress(String address)
{
this.address = address;
}
//Mutator for username
public void setUsername(String username)
{
this.username = username;
}"This" is to access components in that class, and "this" is an old Java term (version 1), the latest version can also do without this.
This method has no return value, because this method is a parameter setting that will be used by its private field correspondent. For example, now we will try to modify the values in the address and username for a person;
public class PersonExample
{
public static void main(String[] args)
{
Person dave = new Person("Dave", "Bob Bill", "Davidson", "12 Pall Mall"); dave.setAddress("256 Bow Street"); dave.setUsername("DDavidson");
}
}Why Use Accessors and Mutators?
The important thing to remember is that we can hide as much data from the object as possible. The extra buffer provided by this method allows us to:
- How data can be changed behind the scenes
- Apply validation to the values we have set in the fields.
Let's say we are going to modify the storage format of middle names. Not just one String but we try to use an array of Strings;
private String firstName;
//Now using an array of Strings
private String[] middleNames;
private String lastName;
private String address;
private String username;
public Person(String firstName, String middleNames, String lastName, String address)
{
this.firstName = firstName;
//create an array of Strings
this.middleNames = middleNames.split(" ");
this.lastName = lastName;
this.address = address;
this.username = "";
}
//Accessor for middleNames
public String getMiddlesNames()
{
//return a String by appending all the Strings of middleNames together
StringBuilder names = new StringBuilder();
for(int j=0;j < (middleNames.length-1);j++)
{
names.append(middleNames[j] + " ");
}
names.append(middleNames[middleNames.length-1]);
return names.toString();
}The implementation into the object has been changed but accessing the class outside of it cannot be affected, this is what we call the "remains exactly the same" method:
public class PersonExample
{
public static void main(String[] args)
{
Person dave = new Person("Dave", "Bob Bill", "Davidson", "12 Pall Mall"); System.out.println(dave.getFirstName() + " " + dave.getMiddlesNames() + " " + dave.getLastName());
}
}Or, let's say an application uses a Person object and can only accept a username of that person with a maximum of 10 characters, then we can add a validation mutator setUsername to ensure that the username entered meets the requirements.
public void setUsername(String username)
{
if (username.length() > 10)
{
this.username = username.substring(0,10);
}
else
{
this.username = username;
}
}Now, if the username entered exceeds 10 characters, it will be automatically truncated.
Reference:
Mutators are used to change the value of a field/data from an object. In addition, it is also intended for encapsulation (instance variable data protection). The method begins with set.
Meanwhile, the access modifier is a keyword used to specify the access level specifications of a variable or method (class member).
- Default
- Public
- Protected
- Private
Default Modifier Access
This type requires that only classes in the same package have access to variables and methods in the class. There are no keywords in this type. Example;
public class StudentRecord{
//akses dasar terhadap variable
int name;
//akses dasar terhadap methode
String getName(){
return name;
}
}Public Access
This type allows all class members to be accessed from both inside and outside the class. Any object that interacts with the class has full access to members of this type. Example;
public class StudetRecord{
//akses dasar terhadap variable
public int name;
//akes dasar terhadap method
public String getName(){
return name;
}
}Protected Access
This type only allows class members to be accessed by methods in that class and subclass elements. Example;
public class StudentRecord{
//akses pada variable
protected int name;
//akses pada method
protected String getName(){
return name;
}
}Private Access
This type allows class access only to the class in which this type is created. Example;
public class StudentRecord{
//akses dasar terhadap variable
private int name;
//akses dasar terhadap method
private String getName(){
return name;
}
}