This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes serilizable objects and variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "serilizable objects and variables" Watch "serilizable objects and variables" New topic

serilizable objects and variables

Neha Mohit
Ranch Hand

Joined: Apr 25, 2006
Posts: 87
Hi ranchers

which variables / objects other than transient cannot be serialized by the standard serilization mechanism

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37939
Anything whose class doesn't implement the Serializable tagging interface. Static fields.
Neha Mohit
Ranch Hand

Joined: Apr 25, 2006
Posts: 87
Originally posted by Campbell Ritchie:
Static fields.

I agree with the first part , but why cant we serialize a static variable ?

Harish Tiruvile
Ranch Hand

Joined: Dec 01, 2005
Posts: 99
Neha, Static variables are serilizable..
Already this query is discussed in this forum...

Giving up is the easiest thing in the world to do..but holding it together when everything seems like falling apart is true strength!!
with regards, Harish.T
Harish Tiruvile
Ranch Hand

Joined: Dec 01, 2005
Posts: 99
Neha, Static variables are serilizable..
Already this query is discussed in this forum...
Nitesh Kant

Joined: Feb 25, 2007
Posts: 1638

Originally posted by harish thrivile:
Static variables are serilizable..

I think you meant they are not serializable.

apigee, a better way to API!
Reji RNair

Joined: Feb 04, 2008
Posts: 6
Hi friends,
Static variable are not serializable in exact sense.
1.We can have a non serializable static variable in a serializable context which is serializable. But if we have a non static Instance variable while serailzation it will thorw a
2.One may think that static varaibles are serializable coz we are getting the values set to the static vaeriable after desrialization. Actualy we get the current value of the static variable which is not instance specific. There are only one static variable per JVM. It is initialized while loading the class and gets updated each time through instance variables or calling static methods of the class. If we deserialize the object in another JVM we will get the initial value of the static variable.

I am attaching the source code depicts the above two points

package Serialization;
* @author Reji R
class SerializableClass implements Serializable {
static int variable=10;
// Instance variable
String instanceValue;
//Not serializable instance .Try here making the variable static by un commenting next line
//static NotSerializable notSerializable=new NotSerializable();
NotSerializable notSerializable=new NotSerializable();
void setValue() {
variable = 99;
public void setInstanceValue(String value){


public class LoadSer {
public static void main(String[] args) throws Exception {
//Change the static variable value after saving the object
File inFile = new File("file.txt");
ObjectInputStream in = new ObjectInputStream(new FileInputStream(inFile));
SerializableClass serializableObject = (SerializableClass)in.readObject();
System.out.println("Static variable value ="+serializableObject.variable);
System.out.println("Instance Variable value="+serializableObject.instanceValue);
* @param args
* @throws Exception
private static void createAnObjectandSave() throws Exception {
SerializableClass serializableClass = new SerializableClass();
serializableClass.setInstanceValue("Jame Bond");
File outFile = new File("file.txt");
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(outFile));

* The class which is not serializable
* @author a-2383
class NotSerializable{

Thanks and Regards.<br />Reji R
I agree. Here's the link:
subject: serilizable objects and variables
Similar Threads
about & system.out
boolean code question
Objects and References
implementation for static and non-static methods stored in memory
variables in Threads