Tino Intro To Java

Last modified: January 05, 2026

Getters and Setters

In CodeHS, when you wanted to get the color of a Rectangle named rect, you would write rect.getColor(). If you wanted to set the color to red, you would write rect.setColor(Color.RED). The getColor() function is an example of a getter because it is a function that returns the value of a property. The setColor(color) function is an example of a setter because it sets the value of a property.

Why use getters and setters in Java?

In Java, it is good practice to define public getters for fields that are meant to be read from other classes (read access) and setters for fields that other classes are supposed to be able to change (write access). In lesson 3, we will learn about access modifiers that can further control access to fields and other parts of a class, giving a class even more exclusive control of its data.

For now, we will just discuss the private access modifier, which makes a field inaccessible from outside the declaring class. By being able to restrict direct access to data, a programmer can then give limited access to the data through the use of getters and setters. Not only does this protect the data from being corrupted from misuse, but it also allows a programmer to define limits on the values a field can have and can allow relationships between different parts of the class to be defined.

For example, if the only way to change the width of a rectangle is through the setter, the setter method can contain logic that restricts the value of the width to only positive values. There could even be code that automatically updates the graphical representation of the rectangle every time the width changes. A Rectangle class could even maintain a list of other objects that are interested in the width of that rectangle and every time the width changes, the setter could call a method on those other objects so they can respond to the change. This opens up the door to very powerful features which would be broken if a programmer using the Rectangle class could just directly change the width by writing rect.width = 100.

Rectangle Example

Here is an example of a simple Rectangle class that utilizes getters and setters.

public class Rectangle {

    // private instance variables that store
    // the width and height of the rectangle
    private int width;
    private int height;

    //A default constructor
    public Rectangle() {
        width = 1;
        height = 1;
    }

    // A getter for width. Returns the value of width.
    public int getWidth() {
        return width;
    }

    // A setter for width. Sets the value of width.
    // Limits the value of width to positive values.
    public void setWidth(int w) {
        if (w > 0) {
            width = w;
        }
    }

    // A getter for height. Returns the value of height.
    public int getHeight() {
        return height;
    }

    // A setter for height. Sets the value of height.
    // Limits the value of height to positive values.
    public void setHeight(int h) {
        if (h > 0) {
            height = h;
        }
    }

    // a combined setter for width and height
    // sometimes setters will set two values at once
    public void setSize(int w, int h) {
        setWidth(w);
        setHeight(h);
    }
}

Note that the logic in setWidth(int w) and setHeight(int h) ignores requests to change the height or width to a non-positive integer. Eventually you will learn how to throw exceptions when invalid values are passed into a method, but for now we will just make the method silently ignore invalid requests.

In the setSize(int w, int h) method, we could have copied the logic from the setWidth(int w) and setHeight(int h) methods, but that would be bad practice because it could lead to inconsistent behavior. By calling the setWidth(int w) and setHeight(int h) methods from inside the setSize(int w, int h) method, we guarantee consistent behavior across all the methods that change the height and width.

Calling an instance method on an object

When you call an instance method, you always have to call the method on an object. If you write obj.doStuff(), you are calling doStuff() on obj. If you write rect.getHeight(), you are calling getHeight() on rect.

The this keyword

To access the object a method or constructor was called on from within that method, you can use the this keyword. You will find this quite handy in multiple scenarios.

Normally, when you call an instance method like the setWidth(int w) method, you have to call it on an object as in: rect.setWidth(5). If you only write setWidth(5), that is the same as calling this.setWidth(5), where this is a reference to the object the method you are in was called on. So when setWidth(w) is written inside the setSize(int w, int h) method, it is the same as writing this.setWidth(w).

Lets look at an example:

    Rectangle wrecked = new Rectangle();
    wrecked.setSize(3, 4);
  • In the first line, wrecked is initialized to an instance of a Rectangle with width 1 and height 1.
  • In the second line, setSize is called on wrecked, passing 3 for w and 4 for h.
  • Inside the setSize method, setWidth(3) is called which is the same as this.setWidth(3), where this is a reference to the same Rectangle wrecked is referencing because setSize was called on wrecked. Thus, the rectangle wrecked is referencing will have its width set to 3.

The this keyword also applies to instance variables. In the setWidth method, when it says width = w, that is the same as this.width = w. Using the this keyword to access instance variables can come in handy if you name a method or constructor parameter the same as an instance variable.

Here is an alternative way we could have written the setWidth method that uses this.width to refer to the instance variable width and just width to refer to the local variable declared in the parameter. Clearly writing width = width would not make sense (both would refer to the local variable), but this.width = width makes perfect sense.

    public void setWidth(int width) {
        if (width > 0) {
            this.width = width;
        }
    }

Dark Mode

Outline