Best Practices: When to Use the Annotation
You'll learn when to explicitly add @FunctionalInterface to your interfaces versus relying on Java's implicit functional interface recognition.
The Core Decision
Java doesn't require @FunctionalInterface for an interface to be functional—any interface with exactly one abstract method automatically qualifies. However, adding the annotation is a communication and safety tool. Think of it like adding @Override: the code works without it, but using it prevents mistakes and clarifies intent.
When You SHOULD Use @FunctionalInterface
Always annotate when you intend the interface to be used with lambdas:
@FunctionalInterface
public interface DataValidator {
boolean validate(String data);
}
This tells developers "this interface is designed for lambda expressions" and prevents accidental addition of extra abstract methods during maintenance.
Especially important for public APIs:
If your interface is part of a library or shared codebase, the annotation serves as documentation and a compiler-enforced contract. Breaking changes trigger compilation errors immediately rather than causing runtime surprises.
When You CAN Skip It
For private, internal interfaces with obvious single-method intent:
Small, local interfaces used only within a single class may not need the annotation if their purpose is crystal clear. However, adding it costs nothing and provides free validation.
For interfaces you don't control:
You can't add @FunctionalInterface to interfaces from external libraries, but they still work with lambdas if they meet the SAM rule.
The Golden Rule
When in doubt, use it. The annotation provides compiler validation, improves readability, and signals your design intent to future maintainers (including yourself). The only cost is typing 22 characters.
Key Takeaway: Use @FunctionalInterface whenever you design an interface intended for lambda use, especially in public APIs—it prevents breaking changes and clearly documents your intent at zero runtime cost.