• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Serialization

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

import java.io.*;
class TestSer
{
public static void main(String[] args)
{
SpecialSerial s = new SpecialSerial();
try{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("fileWriter1"));
oos.writeObject(s);
oos.close();
System.out.print(++s.z +":");

ObjectInputStream ois = new ObjectInputStream(new FileInputStream("fileWriter1"));
SpecialSerial s2 = (SpecialSerial)ois.readObject();
ois.close();
System.out.print(s2.y +":"+ s2.z);
}
catch(Exception x)
{
System.out.println("exc");
}

}
}
class SpecialSerial implements Serializable
{
transient int y = 7;
static int z = 9;
}

My doubt is how come output is "10:0:10",why is it not "10:7:9".I am applying the rule that transient and static variables cannot be serialized.Please help me.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you serializing a transient variable it will store default value instead of actual value.
For y default value 0 is stored instead of 7.

so it is displaying 0.
 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since transient and static not saved with serialized object,
when deserialized ,the transient variable when recovred
assumes null values that corresponds to data type.like for int its 0 ,if it was object reference ,it wold be null.
If output of transient var was 7,as per your code,..that means transient var is also saved.which is nt possible
 
radhika ayirala
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies.I just have one more doubt.Lets say when transient,static variables are tried to serialize,then their default values are stored.Then the ouput of above program should be 10:0:10.How come its 10:0:10.Please help me.

-Radhika
 
raghu nagabandi
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we will never apply transient keyword to static and final variables. if you apply also there will be no effect on that variables. why because if you apply or not , it's actual value will be stored in the file.

where as normal variables if you apply transient keyword it will store default values instead of actual velue.
 
radhika ayirala
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am really not getting the point.Let me be more clear.In the above program,the output is "10:0:10".Please explain each ouput.Can we serialize static variable.I think its 'no'.please try to run above program.In the above program,if i change the 'static int to int',the output is "10:0:9".If 'int' can be serialized,Why is not "10:0:10".

-Radhika
 
raghu nagabandi
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have skype id. i will explain you
 
raghu nagabandi
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out.print(s2.y +":"+ s2.z);

Here your displaying s2.y and s2.z

Here z is a static variable, so it will not depends on instance variable. directly it will display the modified value that is 10.

where as y is the instance variable. when your serializing the y value will be stored as 0 instead of 7.so when you retrived from a file again you will get 0 not 7.

so the output is 0 10
 
I just had the craziest dream. This tiny ad was in it.
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic