Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

serilizable objects and variables

 
Neha Mohit
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers

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


regards
 
Campbell Ritchie
Sheriff
Pie
Posts: 48961
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anything whose class doesn't implement the Serializable tagging interface. Static fields.
 
Neha Mohit
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Harish Tiruvile
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by harish thrivile:
Static variables are serilizable..

I think you meant they are not serializable.
 
Reji RNair
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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{
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic