Concepts / Access Control and Getter/Setter Methods

Access Control and Getter/Setter Methods

A getter method provides read access to a variable by returning its current value to the caller.

  • Programming

A Book That Keeps Its Own Title

Imagine a class that stores information about a book. The class keeps the book's title inside a member variable. Other parts of a program often need to know that title, but they should not have to reach into the class and grab the variable directly. Instead, the class can offer a method whose job is to hand back the stored value. That kind of method is called a getter, and it is the starting point for this article.

What do you think happens?

A book object was created with the title Dune. Another part of the program calls the getter for the title. What does the caller receive?

  • The title stored in the object when the getter runs

  • The word title, because that is the name of the variable

  • Nothing, because the caller cannot read the variable

Reveal answer
Answer:

The caller receives the title stored in the object at the moment the getter runs, which is Dune.

The caller never touches the title variable directly. It asks the getter, and the getter returns whatever value the field holds at that moment. The caller receives the value, not the name of the variable.

Keep the book example in mind as you read. The goal is to see how a method can give other code a way to read a variable's value, how that method is named, and where things commonly go wrong when you write one yourself.

Tracing a Title Through a Getter Call

A getter is a method whose purpose is to return the current value of a variable. Because it only passes a value out, programmers describe it as providing read access to that variable. The method reads the variable on behalf of the caller and returns what it finds there.

Caller codeasks for the titlegetTitle()public getter methodtitle fieldholds the stored value1. call getTitle()2. read title3. current value4. return value
What happens step by step when code calls getTitle(), and which object returns the value to the caller?

Follow the diagram from top to bottom. The caller starts the process by invoking the method. The getter reads the title field, and the value flows back out through the return. The getter is the object that hands the value to the caller. Nothing in this path writes to the field, which is exactly what read access means.

Why Expose a Variable Through a Method

Definition

A getter method is a method that returns the current value of a variable so that other code can read it without accessing the variable directly.

The idea of read access is worth holding onto. A getter is a window for looking at a value. The caller receives the value the variable holds at the moment the method runs. When you read a getter, think of it as a question the caller asks and the class answers with the stored value. The variable itself stays inside the class, and the getter is the only door the outside world uses to read it.

Outside codeother part of the programgetTitle()public methodtitleprivate member variablecallsreads
How can outside code read a value it cannot access directly, and what sits between the caller and the variable?

The diagram shows the layering. Outside code can see and call the public getter, but the field sits behind it. The getter is the only route by which the caller learns the value. This is the core purpose of access control in this pattern: the class decides what outside code may read, and it exposes that value through a method rather than through the variable itself.

Naming a Getter from Its Variable

Java programmers follow a naming convention for getters. The name is not chosen freely. It is derived from the variable it reads, so anyone who knows the variable's name can guess the getter's name. The derivation has two mechanical steps: capitalize the first letter of the variable name, and add get in front of it. Applied to title, the first letter becomes T and the result is getTitle().

getprefix added in frontTitlefield name with capitalfirst letter()no parameters
How does a field name such as title map to the exact getter method name the caller must use?
java
Notes on the code
  • private String title keeps the field hidden from outside code
  • public String getTitle() declares a public method that returns a String
  • return title hands back the current value stored in the field
Worked example

Calling getTitle() on a Book

A Book object is created with the title Dune. What value does a caller receive from getTitle()?

  1. 1
    Create the object

    The constructor runs and stores Dune in the private title field.

  2. 2
    Call the getter

    The caller invokes getTitle() on the object instead of reading title directly.

  3. 3
    Read the field

    The return statement reads the current value of title, which is Dune.

  4. 4
    Receive the value

    The caller receives Dune and can use it, but the stored field is unchanged.

Result

The caller receives the string Dune, and the Book object still holds Dune in its title field.

Repeated Reads Leave the Value Unchanged

Because a getter only reads, calling it several times in a row returns the same value each time as long as nothing else changes the field. The comparison below shows the field before and after a sequence of getter calls.

Before
Book b = new Book("Dune");
String first = b.getTitle();

title holds Dune; first holds Dune

After
Book b = new Book("Dune");
String first = b.getTitle();
String second = b.getTitle();

title still holds Dune; second also holds Dune

The second call changes nothing in the object. The field still holds Dune, and the caller simply receives that value again. This is the practical meaning of saying that a getter only reads the variable.

Mistakes Beginners Make with Getters

Getters are short, so their mistakes tend to be small but still break the pattern. The common errors fall into naming, signature, and body problems.

  • Common mistake

    Using the wrong capitalization or prefix in the name

    What it looks like
    public String gettitle() instead of getTitle()
    Why it fails

    The convention capitalizes the variable name and adds get in front, so callers that follow the convention will not find this method.

    Do this instead

    Capitalize the first letter of the variable name and write getTitle().

  • Common mistake

    Using get for a boolean variable

    What it looks like
    public boolean getDone() for a field named done
    Why it fails

    For boolean variables the convention replaces get with is, so the getter should be named isDone().

    Do this instead
    Name the boolean getter isDone().
  • Common mistake

    Declaring the getter as void

    What it looks like
    public void getTitle() { return title; }
    Why it fails

    A void method cannot return a value, so the caller has nothing to receive. A getter exists to pass a value out.

    Do this instead

    Declare the return type to match the field, such as public String getTitle().

  • Common mistake

    Returning the wrong value

    What it looks like
    return "Unknown"; inside getTitle()
    Why it fails

    The getter should return the current value of the variable it reads, not a fixed or unrelated value.

    Do this instead

    Return the field itself, as in return title;.

  • Common mistake

    Making the field public and skipping the getter

    Why it fails

    When the field is public, outside code reads it directly and the class no longer controls the read path through a method.

    Do this instead

    Keep the field private and expose it through a public getter.

  • Common mistake

    Changing the value inside a getter

    Why it fails

    A getter is meant only to read. Changing the stored value inside it breaks the read-only meaning the caller relies on.

Writing a Getter for Your Own Field

EASY

A class Player has a private int field named score. Write the signature of a public getter for score, giving its name and return type. Then write the signature of a getter for a private boolean field named active.

Hints
  • Start from the field name score and capitalize the first letter, then add get.
  • The return type should match the field type, which is int.
  • For a boolean field, replace get with is, giving isActive().

Key Takeaways

  • A getter method returns the current value of a variable, giving other code read access without direct access to the variable.
  • The standard Java getter name is formed by capitalizing the variable name and adding get in front, so title becomes getTitle().
  • For boolean variables the is prefix replaces get, so a boolean named done is read by isDone().
  • Getters only read the variable; calling one does not change the stored value.
  • Common mistakes include wrong naming, using get for booleans, declaring a void return type, returning the wrong value, and exposing the field directly.