static members belong to the class, not instances—shared state and utility methods. final prevents reassignment (variables), overriding (methods), or subclassing (classes).
Common patterns
class Config {
static final int MAX_USERS = 100;
static int getMaxUsers() {
return MAX_USERS;
}
}
Constants
public static final fields are constants—name in UPPER_SNAKE_CASE. Enums often replace string constants for type safety.
Important interview questions and answers
- Q: Can static methods access instance fields?
A: No—not without an instance reference. - Q: final class?
A: Cannot be extended—String is final.
Self-check
- Why is main declared static?
- What does final mean on a reference variable?