Master Getter and Setter in Java with Simple Example Code

Robert Jul 13, 2026

Getters and setters are fundamental concepts in Java, used to encapsulate fields or instance variables. They are accessor and mutator methods that control how the exterior code interacts with object fields. Let's explore what these methods are and understand their role in Object-Oriented Programming (OOP) with examples.

β˜• Java Basics Cheat Sheet for Beginners | Learn Java in One Page πŸš€
β˜• Java Basics Cheat Sheet for Beginners | Learn Java in One Page πŸš€

In Java, we can't directly access or change the values of instance variables or fields. To do so, we define public methods known as getters and setters. Getters are used to access or 'get' the values of the field, while setters are used to modify or 'set' the values of the field. This way, we ensure the encapsulation principle of OOP, making our code secure and robust.

30 Days of Java – Day 18 | Java Encapsulation Explained for Beginners
30 Days of Java – Day 18 | Java Encapsulation Explained for Beginners

Getters and Setters

As mentioned, getters and setters are methods that provide access to, and manipulate, the values of instance variables. They are declared in the class and usually prefixed with 'get' and 'set', respectively. Let's dive into the details and understand how to use them with examples.

Java Collection Framework: ArrayList Explained Visually
Java Collection Framework: ArrayList Explained Visually

Getters (Accessor Method)

A getter method is used to read or retrieve the value of an instance variable. It's named starting with "get" followed by the field name in camelCase. For example, for a field "name", the getter method would be named "getName". Here's a simple Java class to illustrate this:

Java Variables Explained | Local, Static & Instance Variables in Java
Java Variables Explained | Local, Static & Instance Variables in Java


public class User {
    private String name;

    public String getName() {
        return name;
    }
}

Here, we have encapsulated the 'name' field as private, and provided a public getter method to access its value. You can access 'name' with "user.getName()".

Setters (Mutator Method)

Java Cheatsheet
Java Cheatsheet

Now, let's look at the setter method. A setter is used to change the state of an object by modifying its field's value. It's named starting with "set" followed by the field name. For instance, a setter for the 'name' field would be named "setName". Here's how you might implement it:


public class User {
    private String name;

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

Here, 'setName' is a setter method that allows you to set the 'name' field. You'd use it like this: "user.setName("John Doe")"

Encapsulation in Java (with Realtime Example) - Scientech Easy
Encapsulation in Java (with Realtime Example) - Scientech Easy

Shorter Syntax with Java Bean Convention

Since Java 8, you can also use the shorter syntax for getters and setters using Java's 'Bean' convention. It automatically generates the methods for you, resulting in more concise code.

Advance Java Tutorial - Introduction to JSP and Servlet  https://youtu.be/LjbtQr5shEE Servlet Life Cycle Steps, Java Servlet Tutorial Pdf, Java Server Pages Tutorial, Beginner Guide To Servlets, Java Servlet Configuration, Java Access Specifiers Diagram, Learn Java Jsp, Java Jsp Html, Understanding Java Access Levels
Advance Java Tutorial - Introduction to JSP and Servlet https://youtu.be/LjbtQr5shEE Servlet Life Cycle Steps, Java Servlet Tutorial Pdf, Java Server Pages Tutorial, Beginner Guide To Servlets, Java Servlet Configuration, Java Access Specifiers Diagram, Learn Java Jsp, Java Jsp Html, Understanding Java Access Levels
Java Constructors Explained | Easy Cheat Sheet for Beginners
Java Constructors Explained | Easy Cheat Sheet for Beginners
Top Features of Java Every Beginner Should Know β˜•πŸš€
Top Features of Java Every Beginner Should Know β˜•πŸš€
the cover of an ebook with text that reads, increment & decrement operators
the cover of an ebook with text that reads, increment & decrement operators
Java Methods - A Complete Guide to Master the Concept
Java Methods - A Complete Guide to Master the Concept
Learn Java Collections Fast πŸš€ | ArrayList HashMap Set Queue Explained
Learn Java Collections Fast πŸš€ | ArrayList HashMap Set Queue Explained
Java Generics - An Exclusive Guide on Java Generics
Java Generics - An Exclusive Guide on Java Generics
Data Types in Java
Data Types in Java
Java Programming CheatsheetIntro to Java Programming: An Interdisciplinary Approach and Computer Science: An Interdisciplinary Approach by Sedgewick and Wayne
Java Programming CheatsheetIntro to Java Programming: An Interdisciplinary Approach and Computer Science: An Interdisciplinary Approach by Sedgewick and Wayne
Encapsulation in Java Explained Simply | OOP Concept with Example πŸ’»βœ¨
Encapsulation in Java Explained Simply | OOP Concept with Example πŸ’»βœ¨
Top 5 Java Projects for Beginners
Top 5 Java Projects for Beginners
Java String Methods
Java String Methods
Encapsulation In Java
Encapsulation In Java
Java Patterns Notes 1
Java Patterns Notes 1
Complete Java Cheat Sheet for Beginners to Advanced Developers
Complete Java Cheat Sheet for Beginners to Advanced Developers
How to Work With JSON in Java?
How to Work With JSON in Java?
Java Data Types Explained
Java Data Types Explained
Abstract Class Vs Interface in Java
Abstract Class Vs Interface in Java
Best Java Training in Chennai|Java Course Chennai - Enroll Now
Best Java Training in Chennai|Java Course Chennai - Enroll Now

Automatic Getter Generation

To generate a getter for a variable, simply use the getter method of the class, passing the variable’s name as an argument. Here's an example:


public class User {
    private String name;

    public String get(String name) {
        return this.name;
    }
}

This will generate the getName method automatically.

Automatic Setter Generation

For setters, use the setter method of the class, passing the variable’s name and a value as arguments. Here is how you would generate a setter:


public class User {
    private String name;

    public void set(String name, String value) {
        this.name = value;
    }
}

This will generate the setName method, with the given value to set the 'name' field.

The final part you should take away is that getters and setters are not just handy syntax in Java, but they form the basis of the principle of encapsulation - one of the fundamental principles of Object-Oriented Programming (OOP). They ensure our data is safe and usage is controlled. Happy coding!