TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
@FunctionalInterface annotation is an informative, type-level annotation introduced in Java 8 that indicates an interface declaration is intended to be a functional interface. It instructs the Java compiler to enforce the Single Abstract Method (SAM) rule, triggering a compilation error if the annotated interface contains more or fewer than exactly one abstract method.
Compiler Rules and Mechanics
When the compiler encounters the@FunctionalInterface annotation, it evaluates the interface against a strict set of structural rules. To pass compilation, the interface must adhere to the following:
- Single Abstract Method (SAM): The interface must declare exactly one un-implemented (abstract) method.
- Default Methods: The interface may contain any number of
defaultmethods. Because default methods provide an implementation, they do not count toward the SAM limit. - Static Methods: The interface may contain any number of
staticmethods. Like default methods, these are implemented and do not violate the SAM rule. - Object Class Methods: If an interface declares an abstract method overriding one of the public methods of
java.lang.Object(such asequals,hashCode, ortoString), that method does not count toward the single abstract method limit. This is because any implementation of the interface will implicitly inherit implementations for these methods fromObject.
Technical Characteristics
- Optionality: The annotation is not strictly mandatory. The Java compiler automatically treats any interface meeting the SAM criteria as a functional interface, allowing it to be the target type for lambda expressions and method references. However, applying the annotation acts as a compiler assertion, preventing future modifications from accidentally breaking the SAM contract by adding additional abstract methods.
- Target Restriction: The annotation is meta-annotated with
@Target(ElementType.TYPE). It can only be applied to interface declarations. Applying it to a class, enum, or annotation type results in a compilation error. - Retention: It is meta-annotated with
@Retention(RetentionPolicy.RUNTIME). While its primary utility is compile-time validation, the annotation is preserved in the bytecode and remains accessible at runtime via reflection.
Master Java with Deep Grasping Methodology!Learn More





