I was reading the source of Java’s ArrayList and I came across its backing array declaration:
private transient Object[] elementData;
Why does this need to be transient? Why can’t this class be serialized?
Thanks for the help!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It can be serialized; the
ArrayListclass just takes care of things itself, rather than using the default mechanism. Look at thewriteObject()andreadObject()methods in that class, which are part of the standard serialization mechanism.If you look at the source, you see that
writeObject()does not save the backing array. Instead, it serializes the elements (including null values) one at a time up to thesize()limit. This avoids the overheads of serializing the array, and especially any unused slots at the end of the array. On deserialization, a new backing array of the minimum required size is created byreadObject().