Traditionally, interfaces in Java could not define static methods. In Java 8, however, static methods in interfaces are allowed. A static method stores Java logic and is not implemented by the implementation Java class. You might use a static method to return data meaningful to interface applications such as a list of product categories or a routing list of departments that should receive a report generated by the interface implementation. To learn how to use static methods in Java 8, follow these seven steps.
UseReportInterface, contains the main method that will test the interface implementation. The main method instantiates the interface implementation at line 3 and then calls the static method in order to obtain the routing list. Notice that when calling the static method the interface name (
ReportInterface) is used as the prefix to the method, as opposed to using the object name (
reportInterfaceImpl). Next, the main method calls the implementation methods to produce the report. The second class is the implementation class,
ReportInterfaceImpl, and is defined at line 15. This class provides minimalistic code for each interface method that in the real world would be replaced with Java statements to retrieve and format employee data from a database.