2

I was reading about java annotation and a new doubt appears.

In the documentation explain the FunctionalInterface Annotation Type:

An interface with one abstract method declaration is known as a functional interface.The compiler verifies all interfaces annotated with a @FunctionalInterface that the interfaces really contain one and only one abstract method. A compile-time error is generated if the interfaces annotated with this annotation are not functional interfaces. It is also a compile-time error to use this annotation on classes, annotation types, and enums. The FunctionalInterface annotation type is a marker interface.

I did some test and I did not need to mark my interface with this annotation type. Then, my question is: Why do I need this annotation if every interface with one method is always a functional interface?

Exampe Code

// @FunctionalInterface
interface Wizard {
    int spell(String power);
}

class TestLambda {
    public static void main(String[] args) {
        Wizard gandalf = str -> str.length();
        int power = gandalf.spell(args[0]);
        System.out.println("The spell length is: " + power+ " points");
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Robert
  • 10,403
  • 14
  • 67
  • 117
  • 1
    You posted the answer yourself: "The compiler verifies all interfaces annotated with a @FunctionalInterface that the interfaces really contain one and only one abstract method." – Bubletan Apr 29 '15 at 21:22
  • But it is my doubt? Is this annotation just for that? – Robert Apr 29 '15 at 21:24
  • Yes. It makes sure that it actually is a functional interface. – Bubletan Apr 29 '15 at 21:27

2 Answers2

5

You don't have to annotate a functional interface with @FunctionalInterface but it documents your intention of creating one and will generate a compile error if the interface is not a functional interface (i.e. only has one non default and non static method).

It's a bit like @Override: you don't have to use it but it will prevent you from using a signature that does not match the parent class when overriding a method.

See also: Why isn't @FunctionalInterface used on all the interfaces in the JDK that qualify?

Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783
  • thank you @assylias, but in the final statement of your reference Holger said: >This is exactly like the compiler handles it, you can implement every single abstract method interface using a lambda expression, but when the annotation is present it will ensure that you can use this interface in this way. Then the annotation is used to give the compiler the responsability of check that. – Robert Apr 29 '15 at 21:33
  • I also tried to add a new method to the interface and tried to implement this with a lambda expression and I got the compile-error: is not a functional interface. And I did not use the annotation. – Robert Apr 29 '15 at 21:38
  • You are mixing two things here. You need to separate the provider and the user of the interface. The provider (you) can annotate a functional interface with `@FunctionalInterface`, in which case you will get a compile error if, for example, it has two abstract methods. That's one of the uses of this annotation (apart from documentation). The *user* of the interface (annotated or not) in a lambda expression will get a compile error if that interface is not functional - this is unrelated to the annotation. – assylias Apr 29 '15 at 22:06
  • 3
    In other words, the annotation benefits the provider because it will generate a compile error if the interface is not functional - and it benefits the user because it documents that the interface is meant to be used in lambdas. – assylias Apr 29 '15 at 22:07
0

if you're facing this problem, please import the java.lang.FunctionalInterfacelibrary first before putting the @FunctionInterface like the example below :

import java.lang.FunctionalInterface; 

@FunctionalInterface
public interface GreetingFunction {

    void sayMessage(String message);

}
GameO7er
  • 2,028
  • 1
  • 18
  • 33
ecolas
  • 1