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

Java 9: Interface and @Deprecated [Snippets]

DZone's Guide to

Java 9: Interface and @Deprecated [Snippets]

Let's take a quick look at two pending changes in Java 9: The ability to use Interface and private methods together and an update to the Deprecated annotation.

Free Resource

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

If you followed along with my previous post, you saw the improvements you can expect with Collections in Java 9. Today, we're going to quickly cover how Interface and the @Deprecated annotation will work.

Interface

In Java 8, we can use Interface with static and default methods. Meanwhile, Java 9 will introduce Interface implementations with private methods.

The private method can be accessed only within that Interface.

Example

The interface below has the private method priintInterfaceName(), which will be called by the displayInterfaceName() public method of the interface.

public interface DisplayService{
    public void setUp();
    private static void printInterfaceName(){
        System.out.println("DisplayService");
    }     

    public static void displayInterfaceName(){
        printInterfaceName();
    }
}    


Deprecated Annotation

From 1.5 to Java 8, the @Deprecated annotation has told the user that the method in question is not going to be around in the future and shouldn't expect to see any more use. It gives a warning to avoid using that method's functionality. Java 9 added some more information in this annotation, which will be helpful for developers.

Example

@Deprecated (forRemoval=true , since="9")


Let's break down the parts.

forRemoval

The method forRemoval() will return a Boolean.

If true, it means that this API element is marked for removal in a future release.

If false, the API element is deprecated, but currently, there is no intention to remove it in a future release. The default value of this element is false.

Users are thus given advanced warning to migrate — otherwise, the code is liable to break when upgrading to newer releases.

Since

A method named since() returning a String.

It contains the release number at which it became deprecated.

The default value of this element is an empty String.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:
java ,java 9 ,interface ,deprecation

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}