Sunday, 10 April 2016

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

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...