Concepts / Member Variables and Encapsulation

Member Variables and Encapsulation

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

  • Programming

A Book Whose Title Should Not Be Grabbed Directly

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 the starting point for this article.

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.

What do you think happens?

A small Book class stores a title when the object is created and offers a method that exposes it. When calling code asks for the title, what does it receive?

Reveal answer
Answer:

It receives the value the title field holds at that moment, handed back by the getter method.

The caller never touches the title variable itself. It asks the getter, and the getter returns whatever value the field holds when the method runs. This is the key point of read access.

The Getter as a Window for Reading

Definition

A getter method 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.

The idea of read access is worth holding onto. A getter is a window for looking at a value. The caller receives the value that the variable holds at the moment the method runs. A getter does not describe any other behavior the class might have, so when you read a getter, think of it as a question the caller asks and the class answers with the stored value.

Book classtitle fieldprivate member variablegetTitle() methodpublic getterCalling codecontainsexposesasks for titlereads
What lives inside the object, and what can outside code reach without going through the getter? The title field sits inside the class; the public getter is the only read path shown from outside.

The diagram above shows the boundary. The title field is hidden inside the class, and the getter is the public door through which outside code receives the value. The caller never has to reach into the class and grab the variable directly.

Tracing a Getter Call Step by Step

A getter call has a fixed path. The caller starts the process by invoking the method. The method reads the member variable, and the value flows back out through the return. Nothing in this path writes to the variable, which is exactly what read access means.

Calling codegetTitle()title field1. invoke getTitle()2. read stored value3. current value flows back4. return value
What happens step by step when calling code asks for a variable's value, and where does the returned value come from? The value comes from the title field, passed back through the getter.
  1. 1
    The caller invokes the getter method.
  2. 2
    The getter reads the member variable on the caller's behalf.
  3. 3
    The value the variable holds at that moment is returned to the caller.
  4. 4
    The stored value is unchanged, because a getter only reads.

Deriving a Getter's Name

Java programmers follow a naming convention for getters. The name is not chosen freely. It is derived from the variable it reads, so that anyone who knows the variable's name can guess the getter's name. The derivation has two mechanical steps, which you can apply to any variable.

  1. 1
    Capitalize the first letter of the variable name.
  2. 2
    Add get in front of the result.

Applying these steps to a variable named title gives getTitle(). The source material gives a compact Java getter for the title variable, which reads like a small sentence: it declares a public method, says the method returns a String, gives the method a name, and returns the stored title.

publicmethod is accessible tocallersStringreturn type, matches thevariable typegetTitle()get plus capitalized name
How is each part of a getter's declaration built from the variable's name and type, and which part is the return type versus the method name? The return type String comes from the variable's type; the name getTitle comes from the variable name title.

Writing the Book Getter in Java

The following class follows the book idea. The title field is set when the object is created, and the getter exposes it. The class is a generated illustration built from the source's description, not a copy of a listing from the source.

java
Notes on the code
  • private String title; keeps the field hidden inside the class
  • The constructor sets title when the object is created
  • public String getTitle() is the getter: public, returns String, named get plus Title
  • return title; hands back the current stored value without changing it
java
Output
Dune
Notes on the code
  • The caller never touches title directly
  • getTitle() returns the value the field holds when the method runs
Worked example

Tracing a Boolean Getter

A class has a boolean field done that is currently true. What is the name of its getter, and what does a caller receive when it calls it?

  1. 1
    Identify the variable

    The variable is done, a boolean.

  2. 2
    Apply the boolean rule

    For a boolean, the is prefix replaces get, so the name is isDone().

  3. 3
    Trace the call

    The getter reads done and returns the value it holds at that moment, which is true.

  4. 4
    Check the stored value

    Because a getter only reads, done is still true after the call.

Result

The getter is isDone(), and calling it returns true without changing the stored value.

Choosing Between the get and is Forms

The two prefixes follow the same derivation but apply to different kinds of variables. Comparing them side by side shows what changes and why the is form reads naturally when a getter is used in a condition.

Aspectget formis form
Used forOrdinary variablesBoolean variables
Prefixgetis
Example variabletitledone
Example gettergetTitle()isDone()
Reads naturally asget the titleis it done

The is prefix replaces get for boolean variables, as the source describes.

The is form reads naturally in a condition. A line such as if book.isDone() reads like a question about the book, which matches what a boolean holds. The name follows the same convention as before; only the prefix is different.

Mistakes in Getter Names and Behavior

Getters are simple, so the common errors are about naming and about doing more than reading. Each of the following mistakes is checked against the two rules from the source: the name is derived from the variable, and a getter only reads.

  • Common mistake

    Using the wrong capitalization in the name

    What it looks like
    gettitle() for a variable named title
    Why it fails

    The convention capitalizes the variable name's first letter before adding get.

    Do this instead
    Use getTitle().
  • Common mistake

    Using get for a boolean variable

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

    For boolean variables the is prefix is used in place of get.

    Do this instead
    Use isDone().
  • Common mistake

    Using a different prefix instead of the convention

    What it looks like
    fetchTitle() for the title variable
    Why it fails

    The name is derived from the variable by the get convention, so a caller cannot guess the name from the variable.

    Do this instead
    Use getTitle().
  • Common mistake

    Changing the stored value inside a getter

    What it looks like

    A getter that increments a counter before returning it

    Why it fails

    A getter only reads the variable; calling one does not change the stored value.

    Do this instead

    Return the value without modifying it, and put any change in a separate method.

  • Common mistake

    Returning a value other than the stored one

    What it looks like

    A getTitle() that returns a fixed string instead of the title field

    Why it fails

    A getter returns the current value of the variable it exposes.

    Do this instead

    Return the field itself, as in return title;.

Trace Your Own Getter

MEDIUM

A class stores a boolean variable named active and a String variable named owner. Write the names of both getters using the standard convention, then state what a caller receives from each one if active is false and owner is "Ana". Confirm that neither call changes the stored values.

Hints
  • Apply the is prefix to the boolean and the get prefix to the String.
  • Capitalize the first letter of each variable name before adding the prefix.
  • A getter only reads, so the stored values remain as they were.

Key Takeaways

  • A getter method returns the current value of a variable, giving other code read access without direct access to the field.
  • By convention, a getter is named get plus the variable name with its first letter capitalized, so title becomes getTitle().
  • For boolean variables the is prefix replaces get, so a boolean named done gives isDone().
  • A getter only reads the variable; calling it does not change the stored value.
  • Common mistakes include wrong capitalization, using get for booleans, using a non-convention prefix, and changing state inside a getter.