Tuesday, 24 April 2018

serialization - why Java declared serialVersionUID in Exception and Throwable class?

when I created one custom exception class just like below



public class MyApppException extends Exception {

private String message = null;


public MyApppException() {
super();
}
public MyApppException(String message) {
super(message);
this.message = message;
}
public MyApppException(Throwable cause) {
super(cause);

}
}


compiler giving me below waning




The serializable class InsufficientBalanceException does not declare a static final serialVersionUID field of type long





From Java doc I understand the meaning of serialVersionUID




The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long:




But I did not understand why Java declared serialVersionUID in Exception and Throwable class? What is the use of serialVersionUID in Exception and Throwable class? Is it really required? If yes, Why? Someone please clarify.



Exception




public class Exception extends Throwable {
static final long serialVersionUID = -3387516993124229948L;


Throwable



public class Throwable implements Serializable {
/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = -3042686055658047285L;

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...