Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The @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.
@FunctionalInterface
public interface SimpleFunctionalInterface {
    void execute();
}

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:
  1. Single Abstract Method (SAM): The interface must declare exactly one un-implemented (abstract) method.
  2. Default Methods: The interface may contain any number of default methods. Because default methods provide an implementation, they do not count toward the SAM limit.
  3. Static Methods: The interface may contain any number of static methods. Like default methods, these are implemented and do not violate the SAM rule.
  4. Object Class Methods: If an interface declares an abstract method overriding one of the public methods of java.lang.Object (such as equals, hashCode, or toString), 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 from Object.
@FunctionalInterface
public interface ComplexFunctionalInterface {
    // 1. The Single Abstract Method (SAM)
    void process(String data);

    // 2. Default methods are permitted
    default void log(String msg) {
        System.out.println(msg);
    }

    // 3. Static methods are permitted
    static boolean isValid(String data) {
        return data != null && !data.isEmpty();
    }

    // 4. Overriding public Object methods does not violate the SAM rule
    @Override
    boolean equals(Object obj);
}

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