top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Java: Why we need transient keyword to prevent serialization of a particular data member?

+1 vote
289 views

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.

posted Oct 15, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

static does not just make a member not serialized; it also means that there is only one version of that field for the entire class.

If you want there to be a version 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.

answer Oct 15, 2015 by Salil Agrawal
...