• Post Reply Bookmark Topic Watch Topic
  • New Topic

doubt in serialization  RSS feed

 
abinash das
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I m new java.
I came to know like static variables can't be serialized.
but in a programme i m geting the values of static variable
inside a file.


.java file
-----------
package com.lara;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class TestSerialization implements Serializable
{

static int i=90;
transient double j;
String name;
NewTest t;
public TestSerialization( double j,String name,NewTest t)
{


this.j = j;
this.name=name;
this.t=t;
}

public static void main(String[] args)
{
NewTest t=new NewTest(45,98.99);
TestSerialization si=new TestSerialization(45.98,"abinash",t);
File f=new File("Serial.txt");
FileOutputStream fout=null;
ObjectOutputStream out = null;
try{
fout= new FileOutputStream(f);
out = new ObjectOutputStream(fout);
out.writeObject(si);
}
catch(IOException ex)

{
ex.printStackTrace();
}
finally
{
try
{
if(out!=null)
{
out.close();
out=null;
}
}
catch(IOException ex)

{
ex.printStackTrace();
}
try
{
if(fout!=null)
{
fout.close();
fout=null;
}}
catch(IOException ex)

{
ex.printStackTrace();
}
}

File f1=new File("Serial.txt");
FileInputStream fin=null;
ObjectInputStream in =null ;

try
{
fin=new FileInputStream(f1);
in =new ObjectInputStream(fin);
TestSerialization si2=(TestSerialization)in.readObject();
System.out.println(si2.i);
System.out.println(si2.j);
System.out.println(si2.name);
System.out.println(si2.t.k);
System.out.println(si2.t.l);
}
catch(IOException e)

{
e.printStackTrace();
}
catch(ClassNotFoundException ex)

{
ex.printStackTrace();
}

finally
{
try
{
if(in!=null)
{
in.close();
in=null;
}
}
catch(IOException ex)

{
ex.printStackTrace();
}
try
{
if(fin!=null)
{
fin.close();
fin=null;
}}
catch(IOException ex)

{
ex.printStackTrace();
}
}
}
}
class NewTest implements Serializable
{
int k;
static double l;
public NewTest(int k, double l) {

this.k = k;
this.l = l;
}
}

OUTPUT
------
90
0.0
abinash
45
98.99


So, i m confused.
Thanks in advance
 
amitabh mehra
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint:try setting the static variable to some other value before dserialization and chk the result.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem here is that you are reading the serialized object in the same JVM.
Since, static variables are common for the JVM, you will get the variable value as currently exisiting, irrespective of whether you have deserialized the object or not.
Try changing the value of the static field after you are done writing the serialized object.
When you read the serialized object and try to print the modified static field you will get the modified value and not the value that was serialized.

Other way could be read the serialized from a separate JVM i.e. terminate the serializing program and start a new program that just reads from the serialized file.
Just try these things out and i am sure you will be able to find out what is happening
 
Rob Spoor
Sheriff
Posts: 20901
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, please wrap your code in [ code ] and [ /code ] blocks (remove the spaces). That way the indentation will remain.

Second, why do you think the static variable is stored inside the file? Because you can print it? That is because the variable is stored in the class itself.

Perform the following test and see how it works:

Now first run class WriteTest. After that, run class ReadTest. You'll see that test.i will be 2!

This is because static variables are NOT serialized; each JVM has its own values for the variables. As you can see, the first JVM (for WriteTest) has value 1, whereas the second (or ReadTest) will have a value of 2, even after deserializing.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!