Sub topic 13
Static Methods in Interfaces
A static method is a method that belongs to a type itself, not to any object of that type. Since Java 8, an interface can have static methods. They hold helper code and factory code that goes with the interface, so you don't need a separate utility class for it.
Why put a static method in an interface?
Say many classes check text input. They all need the same small check: "Is this text empty?" You could put that check in a separate class, such as ValidatorUtils. But the check belongs with the Validator idea, so it makes sense to keep it inside the Validator interface.
A static method in an interface is good for two jobs:
- Helpers: small tools that are related to the interface.
- Factories: a factory is a method that creates an object and hands it back to you. For example,
Validator.email()returns a ready-made validator.
A worked example
interface Validator {
boolean isValid(String text);
static boolean isBlank(String text) {
return text == null || text.trim().isEmpty();
}
static Validator email() {
return new EmailValidator();
}
}
class EmailValidator implements Validator {
@Override
public boolean isValid(String text) {
return !Validator.isBlank(text) && text.contains("@");
}
}
public class Main {
public static void main(String[] args) {
System.out.println(Validator.isBlank(" ")); // prints: true
Validator v = Validator.email();
System.out.println(v.isValid("[email protected]")); // prints: true
System.out.println(v.isValid("")); // prints: false
}
}Here is what each part does:
isValidis abstract. Every class that implementsValidatormust write it.isBlankis a static helper. It has a body, and you call it asValidator.isBlank(...).email()is a static factory. The code inmainasks the interface for a validator and never has to typenew EmailValidator().
The key rule: call it through the interface name
A static interface method is not inherited. Classes that implement the interface do not get it. You must call it with the interface's own name.
// Validator.isBlank("x"); // OK
// EmailValidator.isBlank("x"); // compile error
// v.isBlank("x"); // compile errorStatic versus default methods
| Feature | Default method | Static method |
|---|---|---|
| Keyword | `default` | `static` |
| Needs an object to run | Yes | No |
| Implementing class gets it | Yes, and can override it | No |
| How you call it | `obj.method()` | `InterfaceName.method()` |
A static interface method is public unless you mark it private. The next chapter covers private methods.
You have already seen this in the Java library. List.of(1, 2, 3) and Comparator.naturalOrder() are both static interface methods.
Exercise
A class Square implements an interface Shape, and Shape declares static Shape unit(). Which of these compiles: Shape.unit(), Square.unit(), or new Square().unit()? Why?
Show answer
Only Shape.unit() compiles. Static interface methods are not inherited by implementing classes, so they cannot be called through the class name Square or through an object. You must use the interface name.
Exercise
Add a static method named lengthAtLeast(String text, int min) to Validator. It should return true when text is not blank and has at least min characters. Show how main would call it.
Show answer
Inside Validator, write: static boolean lengthAtLeast(String text, int min) { return !isBlank(text) && text.length() >= min; } Then call it in main as Validator.lengthAtLeast("hello", 3), which returns true. Inside the interface, isBlank can be called without the prefix.