Java Getters and Setters

Outside Resources:

TutorialsPoint – Encapsulation

TheNewBoston – Getters and Setters


Many Java classes keep their attributes private, which means other classes are not allowed to view or change them.

Example:

public class User {

    //private attributes
    private String firstName;
    private String lastName;
    private String email;

    //no argument constructor
    public User() {
    }
}

Why private variables? The reason is that other classes might not “know” how to properly handle the data, and they might crash your program or mess up your data if they try to directly access the attributes.

Keeping attributes private is part of the encapsulation principle in Object Oriented Programming. Programmers like to separate the parts of their code that have different jobs to avoid bugs and make it easier to modify and reuse code.

Getters and Setters

Unfortunately, this example class above would be pretty useless, because it has no methods (except the constructor), and so it is impossible for an outside classes to do anything with its attributes.

In other words, this doesn’t work with private attributes:

public static void main(String[] args) {
    User u1 = new User();
    
    //This won't work for a private attribute:
    u1.firstName = "Grace";
}

Setters

Setter methods (also called mutators ) can change an object’s attributes. Example setter method:

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

When you see the word “this” in Java, an object is referencing itself. In this case, the object is setting its own firstName.

“this.firstName = firstName” confuses some people. Explanation:

this.firstName is the object’s attribute. We are assigning a value to this attribute.

firstName (right side of equals sign above) is a parameter, or an input variable, of the setter method. This parameter is defined where it says (String firstName) above. This means that when you call the setFirstName() method, you have to include a String (the first name) inside the parentheses (see below).

The method is public to allow other classes to use it as follows:

public static void main(String[] args) {
    //create a User object instance called u1
    User u1 = new User();

    //set the firstName attribute of u1
    u1.setFirstName("Grace");
}

Notice this format when using an object’s method:

object_name.method_name(argument1, argument2, etc)

This object is called u1, and its method is called setFirstName(). This method takes one argument (a String named firstName).

Getter Methods

Getter methods (also called accessors) return the value of one of the object’s parameters.

Example:

public String getFirstName() {
    return firstName;
}

This method is a String method, because it sends a String (a first name) back to the main program.

(By contrast, the setter method was a void method – setters don’t return anything.)

Here’s how a getter can be used in another class:

public static void main(String[] args) {
    User u1 = new User();
    u1.setFirstName("Grace");
    
    //print attribute
    System.out.println("Name: " + u1.getFirstName());
    
    //store attribute into a new variable
    String fn = u1.getFirstName();
}

Note that you can use IntelliJ IDEA to auto-generate getters and setters.