I do not understand that why we need transient keyword to prevent serialization of a particular data member. We can also make it static because static data member can not be serialized. Please guide me if I am wrong!
static
does not just make a member not serialized; it also means that there is only one copy of that field for the entire class . If you want there to be a copy of that field for each object, but do not want that object to be serialized, you need transient
; static
will do something completely different.
Making variables static
without fully understanding this is a massively common source of bugs for new Java developers.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.