Member Variables and Encapsulation
A getter method provides read access to a variable by returning its current value to the caller.
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
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
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.
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.
- 1The caller invokes the getter method.
- 2The getter reads the member variable on the caller's behalf.
- 3The value the variable holds at that moment is returned to the caller.
- 4The 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.
- 1Capitalize the first letter of the variable name.
- 2Add 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.
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.
- 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
Dune- The caller never touches title directly
getTitle()returns the value the field holds when the method runs
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?
- 1Identify the variable
The variable is done, a boolean.
- 2Apply the boolean rule
For a boolean, the is prefix replaces get, so the name is
isDone(). - 3Trace the call
The getter reads done and returns the value it holds at that moment, which is true.
- 4Check the stored value
Because a getter only reads, done is still true after the call.
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.
| Aspect | get form | is form |
|---|---|---|
| Used for | Ordinary variables | Boolean variables |
| Prefix | get | is |
| Example variable | title | done |
| Example getter | getTitle() | isDone() |
| Reads naturally as | get the title | is 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 likegettitle() for a variable named titleWhy it failsThe convention capitalizes the variable name's first letter before adding get.
Do this insteadUse getTitle().Common mistake Using get for a boolean variable
What it looks likegetDone() for a boolean named doneWhy it failsFor boolean variables the is prefix is used in place of get.
Do this insteadUse isDone().Common mistake Using a different prefix instead of the convention
What it looks likefetchTitle() for the title variableWhy it failsThe name is derived from the variable by the get convention, so a caller cannot guess the name from the variable.
Do this insteadUse getTitle().Common mistake Changing the stored value inside a getter
What it looks likeA getter that increments a counter before returning it
Why it failsA getter only reads the variable; calling one does not change the stored value.
Do this insteadReturn 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 likeA getTitle() that returns a fixed string instead of the title field
Why it failsA getter returns the current value of the variable it exposes.
Do this insteadReturn the field itself, as in return title;.
Trace Your Own Getter
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.