aspose file tools*
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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "serilizable objects and variables" Watch "serilizable objects and variables" New topic
Author

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


regards
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
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 ?


Regards
Harish Tiruvile
Ranch Hand

Joined: Dec 01, 2005
Posts: 99
Neha, Static variables are serilizable..
Already this query is discussed in this forum...
http://www.coderanch.com/t/405828/java/java/we-Serialize-Static-Variable


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...
http://www.coderanch.com/t/405828/java/java/we-Serialize-Static-Variable
Nitesh Kant
Bartender

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
Greenhorn

Joined: Feb 04, 2008
Posts: 6
Hi friends,
Static variable are not serializable in exact sense.
Points:
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 java.io.NotSerializableException:
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;
import java.io.*;
/**
*
* @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){
instanceValue=value;
}

}

public class LoadSer {
public static void main(String[] args) throws Exception {
createAnObjectandSave();
//Change the static variable value after saving the object
SerializableClass.variable=55555;
File inFile = new File("file.txt");
ObjectInputStream in = new ObjectInputStream(new FileInputStream(inFile));
SerializableClass serializableObject = (SerializableClass)in.readObject();
in.close();
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");
serializableClass.setValue();
File outFile = new File("file.txt");
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(outFile));
out.writeObject(serializableClass);
out.close();
}

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


Thanks and Regards.<br />Reji R
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: serilizable objects and variables