This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can we Serialize Static Variable  RSS feed

 
sirisha makkapati
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi any one please tell me

Can We Serialize static variable.

Thanks..
 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, apparently you can, despite that is doesn't make sense, but it could cause some serious problems. So it should probably be avoided.

Here is a good explanation of a potential pitfall
[ January 11, 2007: Message edited by: David McCombs ]
 
Shashi Kant
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello Sirisha

Static variables are NEVER saved as part of the object's state, because they do not belong to the object.
Static variables are not part of the instance's state at all, static variables are purely CLASS variables. They have nothing to do with individual instances. serialization applies only to OBJECTS.
 
Guru dhaasan
Ranch Hand
Posts: 126
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do that but will be useless
 
Mohammad Parwez
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shashi Kant wrote:hello Sirisha

Static variables are NEVER saved as part of the object's state, because they do not belong to the object.
Static variables are not part of the instance's state at all, static variables are purely CLASS variables. They have nothing to do with individual instances. serialization applies only to OBJECTS.


this is possible by providing the following two methods in our class

as shown below. writeObject(),readObject().



ok baye...
 
Campbell Ritchie
Marshal
Posts: 55741
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch , Mohammad Parwez

Please always use code tags, which I have added to your code and you can see how much better it looks.
David McCombs, I couldn’t get the link you posted to open. It also has an incorrect certificate. Please check the link for errors.
 
Campbell Ritchie
Marshal
Posts: 55741
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hadn’t noticed that link was posted 5 years ago; that must be why it won’t open.
 
Divya Kumar
Greenhorn
Posts: 16
Eclipse IDE IntelliJ IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried but we can't serialize static field,through normal serialization concept,may be we can do it through exterlization
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!