Boolean Property Getters: is vs get
What you'll learn: How to name getter methods for boolean properties using the is prefix convention instead of get.
When you write getters for boolean properties (fields that hold true or false values), Java has a special naming convention that makes your code read more naturally. Instead of using the standard get prefix, you typically use is for boolean getters.
The is Prefix Convention
For a boolean field, you can name your getter with is followed by the capitalized property name. This creates more readable, English-like code:
public class User {
private boolean active;
private boolean admin;
// Boolean getters use "is" prefix
public boolean isActive() {
return active;
}
public boolean isAdmin() {
return admin;
}
}
Notice how calling these methods reads like natural language:
User user = new User();
if (user.isActive()) { // reads like: "if user is active"
// ...
}
When to Use get for Booleans
The get prefix is still acceptable for booleans, especially when the property name already suggests a question. For example, getHasPermission() would be awkward—hasPermission() is clearer (using has instead of is).
Some developers also use get if the property name starts with "is":
private boolean isEmpty;
public boolean getIsEmpty() { return isEmpty; } // to avoid "isIsEmpty"
The Pattern
- Boolean field: Use
is+ capitalized name - Non-boolean field: Use
get+ capitalized name
Key Takeaway: Boolean getters conventionally use the is prefix (e.g., isActive()) instead of get to create more natural, readable code that flows like English questions.