If I change the base class that extends the Java Exception class, do I need to update the serialVersionUID value?

Consider the following Java exception classes:

public class BarException extends RuntimeException {
    // [...]
}

public class FooException extends BarException {
    private static final long serialVersionUID = -5322002268075295537L;

    // [...]
}

If I want to update the inheritance hierarchy for deletion BarException, so that FooExceptioncomes directly from RuntimeException, is this required to change the value serialVersionUID?

// FooException with updated inheritance hierarchy
public class FooException extends RuntimeException {
    private static final long serialVersionUID = ???;

    // [...]
}
+3
source share
4 answers

Given that the specification is not clear enough to cause confusion and debate, in the absence of a clear answer, the only option is to trust empirical evidence.

, FooException, BarException, RuntimeException, BarException , , - .

:

serialVersionUID , FooException FooException, .

:

  • JDK 1.5.0_07 .
  • FooException int Exception, .
  • BarException RuntimeException.
+2

. " " .

+5

Java 1.5 Serialization Specification , , serialVersionUID .

, BarException, FooException ( RuntimeException).

+2

Technically, yes, but it depends on whether your system is stored in serialized objects, and if you control how the new, reorganized code is deployed.

If you are not performing persistence and you upgrade the entire deployment with a new version of the code, I see no need to change serialVersionUID.

0
source

Source: https://habr.com/ru/post/1717631/


All Articles