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

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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.

Download Modern Java EE Design Patterns: Building Scalable Architecture for Sustainable Enterprise Development.  Brought to you in partnership with Red Hat

Topics:
java ,annotation ,functional interface ,lambdas

Published at DZone with permission of Per-Åke Minborg, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}