Interface-based applications in Java 8

Discover a Java 8 quirk for declaring applications in interfaces as well as in classes

java8
Credit: smsm89/Flickr

What's strange about Java? Every programming language has its quirks and Java is no different. In this post, I present one of Java's oddities, which was introduced to this language in the Java 8 release.

Basing applications on interfaces

A Java application consists of at least one class that declares a main() entry-point method with the following header -- args is optional and can be replaced with another non-reserved identifier:

public static void main(String[] args)

For example, you create a simple SayHello application whose main() method outputs a hello message. The source code appears in Listing 1.

Listing 1. Class-based SayHello application

public class SayHello
{
   public static void main(String[] args)
   {
      if (args.length == 0)
         System.out.println("Hello!");
      else
         System.out.printf("Hello %s!%n", args[0]);
   }
}

Compile Listing 1 as follows:

javac SayHello.java

Run the application as follows:

java SayHello Java

You observe the following output:

Hello Java!

You also observe that the main() method must be declared in a class in order to create an application. Although that was once true, it's no longer necessary for main() to exist in a class. Check out Listing 2.

Listing 2. Interface-based SayHello application

public interface SayHello
{
   public static void main(String[] args)
   {
      if (args.length == 0)
         System.out.println("Hello!");
      else
         System.out.printf("Hello %s!%n", args[0]);
   }
}

SayHello has been transformed from a class-based application to an interface-based application by replacing class with interface. Starting with Java 8, you can compile the source code and run the application as I previously showed.

Java 8 extended interface types to also support default and static method declarations. Consequently, you can declare the main() entry-point method as an interface member, which leads to creating interface-based application types.

Don't expect an interface-based application type to be fully equivalent to a class-based application type. For example, unlike a class, you cannot instantiate an interface. As a result, you cannot declare non-static fields in an interface. Also regarding fields, you can declare only static constants in an interface: interfaces don't support static fields with mutable state.

Conclusion

It's probably best to avoid this design pattern, or perhaps anti-pattern is more appropriate. It doesn't work with Java versions earlier than Java 8. Also, it has the potential to confuse the heck out of Java beginners.

There's another potential disadvantage. Although this feature works with Oracle's reference implementation of Java 8, perhaps another vendor's Java 8 (or future) implementation might prevent an interface-based application from running.

download
Get the source code for this post's applications. Created by Jeff Friesen for JavaWorld

The following software was used to develop the post's code:

  • 64-bit JDK 8u60

The post's code was tested on the following platform(s):

  • JVM on 64-bit Windows 8.1

This story, "Interface-based applications in Java 8" was originally published by JavaWorld.

Related:
ITWorld DealPost: The best in tech deals and discounts.
Shop Tech Products at Amazon