0

I have a quite weird warning while building our project. The statement is lack of serialVersionUID but in fact the class in question does define such.

Can someone explain the below error?

[WARNING] /build/location/com/our/company/package/SomeClass.java:[178,56] [serial] serializable class <anonymous com.our.company.package.SomeClass$1> has no definition of serialVersionUID

I am not sure what SomeClass$1 means in this case.

Mateva
  • 786
  • 1
  • 8
  • 27
  • There is no error here, only a warning. – user253751 Jul 21 '14 at 08:45
  • 1
    Well it's an anonymous class... so presumably your *named* class provides a serialVersionUID, but the anonymous subclass doesn't. So somewhere you've got `new SomeClass() { ... }`. – Jon Skeet Jul 21 '14 at 08:46
  • 1
    Also, `SomeClass$1` is just the name of the anonymous class - all classes have names in bytecode, and the compiler generates them for anonymous classes (from the name of the enclosing class, a $, and a number). – user253751 Jul 21 '14 at 08:46
  • A simular question has been asked here: http://stackoverflow.com/questions/7823477/warning-serial-serializable-class-someclass-has-no-definition-of-serialversio – Casper Jul 21 '14 at 08:46

1 Answers1

1

SomeClass$1 is the first anonymous class contained in SomeClass. So somewhere in the code for SomeClass, you're using an anonymous class, e.g:

SomeType instance = new SomeType {
    public ReturnType someMethod() {
        // ...implementation...
    }
};

The warning is that the resulting anonymous class has no serialVersionUID, which could lead to serializaton problems. (I guess the base class of your anonymous class must be serializable.)

You can give it one:

SomeType instance = new SomeType {
    private static final long serialVersionUID = 12345678L; // Change number as appropriate

    public ReturnType someMethod() {
        // ...implementation...
    }
};

...although there's some debate (one, two) about whether having serializable nested classes is best practice.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875