Serializable interface with default method

Vladimir Tsanev tsachev at gmail.com
Wed Jan 27 13:59:26 UTC 2016


Hi

I have a simple interface that extends Serializable.

    import java.io.Serializable;
    public interface MyInterface extends Serializable {}

When I try to add a default method to it like this:

    import java.io.Serializable;
    public interface MyInterface extends Serializable {
        default void foo() {}
    }

And compile with with -Xlint

    javac -Xlint MyInterface.java

I get following warning

    MyInterface.java:3: warning: [serial] serializable class MyInterface
has no definition of serialVersionUID
    public interface MyInterface extends Serializable {
           ^
    1 warning

It seems that for some reason the compiler thinks that MyInteface is a
class, while it is not.

I found two possible workarounds.

1) To add @SuppressWarnings("serial") on the interface.

    import java.io.Serializable;

    @SuppressWarnings("serial")
    public interface MyInterface extends Serializable {
        default void foo() {}
    }

2) To add serialVersionUID constant in the interface.

    import java.io.Serializable;

    public interface MyInterface extends Serializable {
        long serialVersionUID = 42L;

        default void foo() {}
    }

While I can live with 1) it is annoying to add a redundant annotation.
2) is pretty ugly because I do not want to add *public* constants to my
interface, that I do not need.


I couldn't find anything in java.io.Serializable javadoc related to
interfaces with default values.
This seems like a bug to me. Is there another good reason for this warning,
that I miss?

Regards,
Vlado
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20160127/90e57967/attachment.html>


More information about the compiler-dev mailing list