Imagine a bacon-wrapped Ferrari. Still not better than our free technical reports.

Being Functional with Java 8 Interfaces and JRebel

In this post, we’ll be expanding on an earlier post from the JRebel 6 beta period. My awesome colleague and Java Champion Anton Arhipov, wrote a blog post about adding and extracting interfaces without redeploying the application. That was awesome, but let’s take a look at some more advanced Interface Shenanigans ™. In this post we’ll not only add new interfaces to our application, but we’ll also dabble in Java 8 default methods and functional interfaces, all without stopping our application thanks to JRebel.

To recap, a really common refactoring method is to extract an interface from an existing class. Proper usage of interfaces is a great way to increase the flexibility of your code. Changing the list of implemented interfaces is a pretty hard thing to do in bytecode, luckily we’ve got a lot of experience doing dabbling in the JVM’s internals.


Let’s start with a console application that has one class, the main method runs a loop with an incrementing counter, and it calls a method that outputs some hello-world-type text.

Here’s our starting code:

public static void main(String[] args) throws Exception {
  int counter = 0;
  while(true) {

public static void sayHello(int count) {
  System.out.println(count + ": sayHello");

Here’s some of our application’s console output, as you can see we’ve enabled JRebel:

2015-02-18 17:29:39 JRebel:  Licensed to Adam Koblentz
2015-02-18 17:29:39 JRebel:  
2015-02-18 17:29:39 JRebel:  License type: dedicated
2015-02-18 17:29:39 JRebel:  Valid from: June 14, 2014
2015-02-18 17:29:39 JRebel:  Valid until: June 13, 2015
2015-02-18 17:29:39 JRebel:  
2015-02-18 17:29:39 JRebel:  
2015-02-18 17:29:39 JRebel:  #############################################################
2015-02-18 17:29:39 JRebel:  
0: sayHello
1: sayHello
2: sayHello
3: sayHello
4: sayHello
5: sayHello
6: sayHello
7: sayHello
8: sayHello

Our counter is dutifully incrementing on each call of our sayHello method. With that working as expected, let’s get a little crazy and not only add a new Interface to our application, let’s take advantage of the new Java 8 functional programming functionality.

With the arrival of Java 8, Functional Programming is all the rage in the Java world and we now have a new annotation for Interfaces that conform to a certain standard. @FunctionalInterface is a cool little annotation when applied to an Interface with exactly one abstract method, you can use it to remove tons of boilerplate from your code. This is great because one of the biggest complaints about Java is the verbosity of simple code, with Functional Interfaces your code will be much more readable.

Let’s add a new Interface called SayHelloInterface and we’ll define it like this:

public interface SayHelloInterface {
  void sayHello();

If you notice we’ve added the @FunctionalInterface annotation and we have exactly one abstract method declared, the IDE or compiler will actually complain if you violate that constraint. Now, traditionally to use the method we’d have to not only create a new instance of a class that implements the SayHelloInterface, we’d have to instantiate it and then call it. With Java 8 and Functional Interfaces, we can do this really cleanly.

Let’s add some code to our sayHello method:

SayHelloInterface heyAnonymousClass = () -> System.out.println("Hello from the Interface's class!");

Now, when we hit save in the IDE, we see JRebel kick into action and in the console we see:

110: sayHello
111: sayHello
112: sayHello
2015-02-18 17:31:32 JRebel: Reloading class 'com.zeroturnaround.blogpost.Main'.
113: sayHello
Hello from the Interface's class!
114: sayHello
Hello from the Interface's class!

From this we can see that the counter’s state is maintained with the reload and the new Functional Interface works great. This means that with JRebel, we can refactor our application in pretty substantial ways, like class hierarchy, without losing state or having to stop and rebuild the application or redeploy on the application server.

One more cool thing in Java 8 is the ability to define default behavior in a method, this can blur the lines for newer programmers between Abstract classes and Interfaces, but it’s a pretty powerful new feature. Let’s add a new default method to our interface like this:

default String helloInterfaceMessage(){
  return "This is from the Interface!";

Notice the default keyword, this means that any class that implements this interface will get this implementation of the helloInterfaceMessage method. We can now call this method without having to define anything extra in our anonymous class from earlier and call it like this:


You can see here that we’re taking the String returned by the method and outputting it to the console, and the console now shows the reload as well as the new method being called.

167: sayHello
Hello from the Interface's class!
168: sayHello
Hello from the Interface's class!
2015-02-18 17:32:28 JRebel: Reloading class 'com.zeroturnaround.blogpost.Main'.
169: sayHello
Hello from the Interface's class!
This is from the Interface!
170: sayHello
Hello from the Interface's class!
This is from the Interface!
171: sayHello
Hello from the Interface's class!
This is from the Interface!

In conclusion, the ability to modify the list of implemented interfaces gives you even more freedom when refactoring your code, all without having to restart your application with JRebel. Whether you’re working on an existing application or rocking new Java 8 code, JRebel will reload your code changes instantly, freeing you to spend more time working on your awesome apps and less time worrying about your tooling.