1

I' m trying to understand @Functional interface from JDK and can't catch why did they create this annotation without a body?

I don't understand what is difference between using @FunctionalInterface in class description and not using? what does this annotation without params provide?

@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface FunctionalInterface {} 
3
  • All functional interfaces are recommended to have an informative @FunctionalInterface annotation. This not only clearly communicates the purpose of this interface, but also allows a compiler to generate an error if the annotated interface does not satisfy the conditions. See baeldung.com/java-8-functional-interfaces Commented Jun 11, 2019 at 10:07
  • 3
    @FunctionalInterface annotation is used to ensure an interface can’t have more than one abstract method. The use of this annotation is optional. Commented Jun 11, 2019 at 10:08
  • It is purely a marker interface, to signal that some annotated interface is intended to be used/usable as lambda, that it contains just one function. Containing one function would mean calling getDeclaredMethods for counting, which is an unnecessary overhead. Commented Jun 11, 2019 at 10:09

1 Answer 1

3

If you add it, someone won't be able to add more methods without breaking your functional interface and will get a compile time error:

Invalid '@FunctionalInterface' annotation; MyInterface is not a functional interface 

If a type is annotated with this annotation type, compilers are required to generate an error message unless:

  • The type is an interface type and not an annotation type, enum, or class.
  • The annotated type satisfies the requirements of a functional interface.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.