• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

doubt in serialization

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
No more fooling around. Read this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic