• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

doubt in serialization

 
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
 
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.
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • 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
 
Sheriff
Posts: 21824
105
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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's just a flesh wound! Or a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!