Standard Getter Method Naming and Structure
What you'll learn: How to write getter methods that follow Java's universal naming conventions and structural rules.
The Standard Pattern
Now that you understand why we use getters and setters to access private fields, let's learn the exact structure Java developers follow for getter methods. This isn't just a suggestion—it's a widely-adopted convention that makes your code instantly readable to any Java programmer.
The Three Rules of Getter Methods
1. Naming: Use the get prefix
A getter method's name always starts with get, followed by the field name with its first letter capitalized.
- Field
name→ MethodgetName() - Field
age→ MethodgetAge() - Field
isActive→ MethodgetIsActive()(or justisActive()for boolean)
2. Return Type: Matches the field type
The getter's return type is identical to the field's type.
String name→public String getName()int age→public int getAge()
3. Parameters: Zero
Getter methods take no parameters. They simply return the current value.
public class Student {
private String name;
private int age;
// Standard getter: public, returns field type, no parameters
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
Why This Matters
This predictable structure allows other programmers (and tools like IDEs) to instantly recognize a property accessor. When you see getBalance(), you know exactly what it does—no guessing required.
Key Takeaway:
Getter methods follow the pattern public <FieldType> get<FieldName>() with zero parameters and a simple return statement—this universal convention makes Java code self-documenting.