Tuesday, May 27, 2008

How do you avoid InvalidClassException for arrays?


Question: Arrays are objects in Java. How do you ensure that you don't get InvalidClassException due to potentially different serialVersionUID across different Java Compiler implementations?


Answer: Java specifications ensure that for us. Nothing specific required for arrays in this regard. Okay... the next question which may come to our mind is:- why the requirement of matching the serialVersionUID is waived for Arrays in Java? The underlying reason for this waiver is that even though Arrays are treated as objects in Java, we don't really have a Class which we can change for adding this new field explicitly, so we can not declare and initialize serialVersionUID field the way we do for any other class we ensure uniform serialization for. Array classes will continue to have only the default computed value for serialVersionUID. And hence the waiver is probably the only solution to avoid a potential InvalidClassException across different Java Compiler implementations.



Share/Save/Bookmark


No comments: