• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

serilizable objects and variables

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers

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


regards
 
Marshal
Posts: 76060
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Neha, Static variables are serilizable..
Already this query is discussed in this forum...
https://coderanch.com/t/405828/java/java/we-Serialize-Static-Variable
 
Harish Tiruvile
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Neha, Static variables are serilizable..
Already this query is discussed in this forum...
https://coderanch.com/t/405828/java/java/we-Serialize-Static-Variable
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


I think you meant they are not serializable.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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{
}
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic