Home > Java > javaTutorial > body text

Java Getter Setter

WBOY
Release: 2024-08-30 16:11:19
Original
495 people have browsed it

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 Tests

Start 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;
}
}
Copy after login

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;
}
}
Copy after login

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.

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; 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;
}
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);
}
}
Copy after login
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;Java Getter Setter

// in case of boolean get is replaced with ‘is’
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
Example for Java Getter Setter

This example shows how to declare and use the getter and setter method for a variable having different data types.

Code:

  1. Output:
  2. Conclusion
The above article provides a clear understanding of the use of getters and setters in java. Using getter and setter is considered as best practice in java development. We use getters and setters in our application as they help create secure, roust and maintainable code. Recommended Article This is a guide to Java Getter Setter. Here we discuss the use of getters and setters in java and its Methods and Example and code implementation. You can also go through our other suggested articles to learn more – Layout in Java Java Compilers Merge Sort In Java Java BufferedReader

The above is the detailed content of Java Getter Setter. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!