Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Java Holiday Calendar 2016 (Day 1): Use the @FunctionalInterface Annotation

DZone's Guide to

Java Holiday Calendar 2016 (Day 1): Use the @FunctionalInterface Annotation

If you want to ensure API users can use lambdas to implement your interface, it's important to make sure your code is annotated properly. Check out how to do it.

· Java Zone ·
Free Resource

The CMS developers love. Open Source, API-first and Enterprise-grade. Try BloomReach CMS for free.

As the holidays draw near, it's important to keep your skills sharp! That's why DZone MVB Per-Åke Minborg is going to be posting a quick tip a day through the holiday season to help keep your code fighting fit. To get things started, here's a lesson about the @FunctionalInterface annotation and why you should use it.Image title

Today's tip is to annotate a functional interface with the @FunctionalInterface annotation, thereby signaling that API users may use lambdas to implement the interface. It also ensures that the interface remains usable for lambdas over time by preventing abstract methods from accidentally being added to the API later on.

Do This

@FunctionalInterface
public interface CircleSegmentConstructor {
    CircleSegment apply(Point cntr, Point p, double ang);
    // abstract methods cannot be added
}


Don't Do This

public interface CircleSegmentConstructor {
    CircleSegment apply(Point cntr, Point p, double ang);
    // abstract methods may be accidentally added later
}


Read more in the original article here on DZone, which goes into more about API Design with Java 8.

Follow the Java Holiday Calendar 2016 with small tips and tricks all the way through the winter holiday season.

BloomReach CMS: the API-first CMS of the future. Open-source & enterprise-grade. - As a Java developer, you will feel at home using Maven builds and your favorite IDE (e.g. Eclipse or IntelliJ) and continuous integration server (e.g. Jenkins). Manage your Java objects using Spring Framework, write your templates in JSP or Freemarker. Try for free.

Topics:
java ,annotation ,functional interface ,lambdas

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}