TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Java

Accessor and Mutator methods in Java

Java Tutorial

Introduction

In this post, we will learn about Accessor and Mutator methods in Java. These methods are used to retrieve and set the instance variables of a class. The methods are also called Getters and Setters respectively.

We want our class can be used by external clients.. Build methods for each private data variable. We need to declare public methods to operate on our private data.

One method to get the value of the private data item -> Getter or Accessor or Get method
One method to set the value of the private data item -> Setter or Mutator or  Set method

For example, see the Getters & Setters defined in the Product class.

Product class

Let’s take the Product class as an example and generate methods for the class. The product class has three instance variables name, price, and quantity.

public class Product {
private String name;
private double price;
private int quantity;
}

 

Accessor method:

We use the Accessor method to get the value stored in the private instance variable of the class. This method is also called the Getter method. This method returns the variable value.

Example:
/**
* @return the name
*/
public String getName() {
return name;
}

Usage:

Product p = new Product();

String productName;

productName= p.getName(); //to get the product name in the p object.

 

Mutator method:

We use the Mutator method to store or change the value of the instance variable of the class. This method is also called the Setter method. This method sets the variable value.

Example:
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}

Usage:

Product p = new Product();

p.setName("Phone");  // to set the product name in the p object.

 

 

Product class

 

Create Accessor and Mutator methods

IDE’s like Eclipse, IntelliJ, etc. will allow creating the methods automatically for you.

 

 

Setter-and-Getter-Methods-Java-Tip

Eclipse tutorial on this website can be found at:

https://www.testingdocs.com/eclipse-tutorials/

Implement getters and setters for instance variables/attributes of your Java class. Eclipse will automatically create them for you with option:

Right-click >> Source >> Generate Getters and Setters.

 

Check the variables for which you want to create the methods. You can do a Select All to create the methods for all the variables in the class.

 

Accessor and Mutator

Check the Generate method comments to generate comments for the methods.

Click the OK button.

You would find the methods and comments created.

 

Setters and Getters Java

 

Code Listing

//Product class

public class Product {
private String name;
private double price;
private int quantity;

/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}

/**
* @return the price
*/
public double getPrice() {
return price;
}

/**
* @param price the price to set
*/
public void setPrice(double price) {
this.price = price;
}

/**
* @return the quantity
*/
public int getQuantity() {
return quantity;
}

/**
* @param quantity the quantity to set
*/
public void setQuantity(int quantity) {
this.quantity = quantity;
}

}

 

Importance of these methods

Learn how to protect the instance variables in the class:

https://www.testingdocs.com/encapsulation-in-java-with-example/

For more information on Java, visit the official website:

https://www.oracle.com/java/

Related Posts

Download Greenfoot Windows

Java /

Download & Install Greenfoot on Windows

Java /

Java Static Code Analysis

Java /

Java Testing Tools

Java /

Handle Multiple Exceptions in Java

Exceptions_In_Java

Java /

Exceptions in Java Programs

‹ Java Program Structure› Differences between final, finally and finalize

Recent Posts

  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • RAPTOR Editions
  • Flowgorithm Conditional Breakpoint Statement
  • Flowgorithm Read Numbers from File Example
  • Search Text File Flowchart Example
  • Flowgorithm Turtle Graphics Symbols
  • Draw Circle using Flowgorithm Turtle
  • Draw Parallel Lines using Flowgorithm Graphics

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version