Getters and Setters in java are two methods used for fetching and updating the value of a variable. Getter methods are concerned with fetching the updated value of a variable, while a setter method is used to set or update an existing variable’s value. Getter and setter methods in java are also named as accessor and mutator, respectively.
Syntax of Java Getter Setter
Below is the syntax of using a getter and setter method in java:
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
package <packagename>; public class <classname>{ // class member variable private <datatype> <variablename>; // getter method to fetch data public <datatype> get<variablename>(){ return <variablename>; } // setter method to update data public void set<variablename>(<datatype> variableName){ this.<variablename>= variableName; } }</variablename></datatype></variablename></variablename></variablename></datatype></variablename></datatype></classname></packagename>
In the above syntax following symbols are used:
-
: Name of the package. -
: Name of the class. -
: Name of the variable. -
: Data Type of the variable.
The main intention of using getter and setter in java is to provide data hiding. Variables of a class are marked as private, and getters & setters are exposed in order to fetch and update the value of that variable. With the use of getters and setters, a developer can control how variables can be accessed and updated in a proper condition. Therefore, getters and setters provide access security to a variable that protects its value from being changed from the outside world. Let us consider an example to make things clear:
Example:
public class Test { //String variable marked as private private String name; // getter method public String getName() { return name; } // setter method public void setName(String name) { this.name = name; } }
In the above example, the variable name is marked as private and getters; setters are exposed. Marking variable as private provides data hiding. Changes to variable name cannot be made from the outside world.
The below-naming convention is followed for naming getter and setter methods:
- get
- set
Methods of Getter & Setter
The below table shows the getter and setter method for the given variable name.
|
Getter Method | Setter Method | ||||||||||||||||||
private int data | public int getData() { return data; } |
public void setData(int data) { this.data = data; } |
||||||||||||||||||
private ArrayList elements;package com.edubca.gettersetterdemo; import java.util.ArrayList; import java.util.Date; import java.util.function.Consumer; public class Test { private int number; public ArrayList languages; private String name; private Date date; private boolean flag; public int getNumber() { return number; } public void setNumber(int data) { this.number = data; } public ArrayList getLanguages() { return languages; } public void setLanguages(ArrayList elements) { this.languages = elements; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; } public static void main(String[] args) { Test test =new Test(); // Creating an instance of class test.setNumber(10); // setting value of Integer Data int output= test.getNumber(); // reading value of Integer Data System.out.println("Value of Integer Data is :" + output); System.out.println(); test.setName("Edubca"); //setting value of String variable String stringOutput= test.getName(); // reading value of String variable System.out.println("Value of String Data is :" + stringOutput); System.out.println(); ArrayList list =new ArrayList(); list.add("Java"); list.add("Hadoop"); list.add("Spark"); list.add("Hive"); list.add("Python"); list.add("C++"); list.add("Machine Learning"); test.setLanguages(list); // set list of languages ArrayList languages=test.getLanguages(); // get list of languages System.out.println("List of languages is : "); languages.forEach(new Consumer<string>() { @Override public void accept(String t) { // TODO Auto-generated method stub System.out.println(t); } }); System.out.println(); Date date =new Date(); test.setDate(date); // setting value of date variable Date outdate=test.getDate(); // reading value of date variable System.out.println("Current Date is : " + outdate); System.out.println(); test.setFlag(true); // set boolean variable boolean outflag=test.isFlag(); // reading boolean variable System.out.println("Flag is :" + outdate); } }</string> |
public ArrayList getElements() { return elements; } |
public void setElements(ArrayList elements) { this.elements = elements; } |
||||||||||||||||||
private String name; |
public String getName() { return name; } |
public void setName(String name) { this.name = name; } |
||||||||||||||||||
private Date date; | public Date getDate() { return date; } |
public void setDate(Date date) { this.date = date; } |
||||||||||||||||||
private boolean flag; |
// in case of boolean get is replaced with ‘is’ public boolean isFlag() { return flag; } |
public void setFlag(boolean flag) { this.flag = flag; } |
This example shows how to declare and use the getter and setter method for a variable having different data types.
Code:
- Output:
- Conclusion
The above is the detailed content of Java Getter Setter. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use