Samstag, 18. Januar 2014

Major bug with Java 8 default methods in interfaces found in JavaFx project

I work on a Swing to JavaFx migration project with the guys of canoo. We reimplement features of existing Swing controls with JavaFx. On implementing additional features for the TableView we found a major bug when moving columns. We filed that bug in the openfx JIRA and had some discussions with the Oracle developers.

On trying a workaround we were told of, a canoo developer found, that it was no JavaFx bug in the TableView, it was a critical bug in the Java 8 VM itself!

If you use the new default interface methods, an interface can provide a default implementation for a method. That means, that an implementing class does not have to implement this method, but it can do. See the example:

public interface MyInterface{
  default int answerToAllQuestions(){
    return 42;

public class MyClass implements MyInterface{


public class MySecondClass implements MyInterface{
  public int answerToAllQuestions(){
    return 21;

The interface MyInterface defines a default method. We have two classes implementing the interface. The first one does not implement the method of the interface, so the default method implementation will be used. The second class implements the method.

The bug is now, that if you have a class overriding a default method, the using of that method instead of the default method is not stable. After some time the VM decides sometimes to use the interface default implementation instead of the method in the implemented class. That can result in weired behaviour, that is really hard to reproduce and find out.

This bug (see the JIRA here) costed us more than 25 days of work, so have that problem in mind, if you use interface default methods in your project. A big problem is, that the collection and the new stream framework use that feature heavily. It will be interesting to see, if this P1 bug will be fixed before the release of the first Java 8 release in March.

The bug was fixed. The cause was an over optimization of the hot spot compiler. So the fix will be part of the official JDK 8 release in March! So we can use the default methods in interfaces again with not being afraid of strange behaviour.

1 Kommentar:

  1. Whoa these days lots of buzz is going on default methods , once java 8 is officially release it i am sure that most of the applications will definitely going to use it.

    The reason why i think till now there was always a constraint that if we want to introduce some new method (prior to java 8) in an interface ,then it can break the whole application as all the implementation will be forced to provide an implementation for that ,so in that case we try to solve this problem by introducing a new interface to include that new method and i think here we violate the Single Responsibility Model (as its not stated anywhere).

    Again I personally feel that Abstract classes have their own cost of using them in terms of multiple inheritance and I also feel that Default methods and abstract classes have there own place in Java and they can't replace each other.

    One more thing I found an interesting interesting article on Default methods here , it is little bit on the sarcastic side but a worth reading.