Introduction to getter and setter in java example

Getter and Setter Method Java Example

 

Java provides a number of ways to read and write the value of an object field. In this article, we'll take a look at two of those ways: getters and setters. We'll also provide a java getter and setter example so that you can see how these two methods are used.

What are the Getter and Setter methods?

Getter and setter methods are used to protect the data in an object by making the data private and providing public methods to allow other classes to access and modify the data. This is known as encapsulation. Getter methods are used to return the value of a private data member, while setter methods are used to assign a new value to a private data member.

For example, let's say we have a class called Person that has private data members for a person's name and age. We would want to create getter and setter methods in order to allow other classes to access and modify this data. Our Person class might look something like this:

public class Person {
// private data members
private String name;
private int age;

// getter method for name
public String getName() {
return name;
}

// setter method for name
public void setName(String newName) {
name = newName;
}

// getter method for age
public int getAge() {
return age;
}

Syntax of Getter and Setter Method

Getter and setter are the two main methods used in Java to protect data encapsulation. A getter is a method used to return the value of a variable, while a setter is a method used to set or change the value of a variable. In order to use getter and setter methods, you need to follow the proper syntax. The following is the general syntax for getter and setter methods in Java:

Getter Method Syntax:

VariableType varName = obj.getVariableName();

Setter Method Syntax:

obj.setVariableName(VariableType varName);

Why Getter and Setter are used?

Getter and setter are used in order to protect the data within an object. By using getters, we can make sure that no one can change the data inside of an object without permission. Setters are used to allowing objects to change their data if necessary. Getters and setters help to keep our code clean and organized.

Getter and Setter Method Java Program

A getter method is a method used to return the value of a variable in Java. A setter method is a method used to set the value of a variable in Java. In this example, we will create a getter and setter method for the variable's name and age.

public class GetterSetterExample {

private String name;
private int age;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {

System.out.println("My age is: "+age);

}

Public Class Test {

Public void main String(args []) {

GetterSetterExample example = new GetterSetterExample()

example.setName("Mathew");

System.out.println("My name is: "+example.getName());

}

}

Bad Practices in Getter and Setter Method

I have seen a lot of bad practices in getter and setter methods in Java code. I will share some of them with you so that you can avoid them in your code.

1. Don't use getters and setters for simple data types:

There is no need to use getters and setters for simple data types like int, float, char, etc. You can directly access the instance variable from outside the class. For example, if you have an int variable named "id", you can directly access it from outside the class like this:

int id = myObject.id;

2. Don't use getters and setters for static data:

Just like you don't need getters and setters for simple data types, you also don't need them for static data. You can directly access the static variable from outside the class. For example, if you have a static variable named "MAX_SIZE", you can directly access it from outside the class like this:

int size = MyClass.MAX_SIZE;

3. Don't use getters and setters to return mutable objects

Code School

Code School is sharing assignments, books, labs, lectures, notes, OOP and java code, projects, and semester projects.

Post a Comment

Previous Post Next Post