-2

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!

1 Answers1

5

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413