• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problems in Serialization...

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
today i got sample question(for ocjp6) and there was a question regarding Serialization and deserialization...
import java.io.*;
public class A{
public static void main(String arg[]){
B b=new B();
b.x1=7;b.x2=7;b.x3=7;//1
//Serialize b and then deserialize b
System.out.println("x1="+b.x1+"x2="+b.x2+"x3="+b.x3);
}
}

class B implements Serializable{
static int x1=0;
volatile int x2=0;
transient int x3=0;
}
i answered x1=7,x2=7,x3=0
but solution says it should be x1=0,x2=7,x3=0
i am not be able to understand that why x1 is not equal to 7 because at 1, x1 has explicitly initialized with 7.
even i write the complete code then after running still i got x1=7,x2=7,x3=0
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
serialize and deserialize should be in different class.
note: please search first. this kind of topics discussed many times here in javaranch
 
Dharmenrda kumar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:serialize and deserialize should be in different class.
note: please search first. this kind of topics discussed many times here in javaranch


sorry i did't got what you want to say...
but the complete code is like...


import java.io.*;
public class A{
public static void main(String arg[]){
B b=new B();
b.x1=7;b.x2=7;b.x3=7;//1
try{
FileOutputStream fs=new FileOutputStream("a.txt");
ObjectOutputStream os=new ObjectOutputStream(fs);
os.writeObject(b);
os.close();
}
catch(Exception e){}
try{
FileInputStream fi=new FileInputStream("a.txt");
ObjectInputStream oi=new ObjectInputStream(fi);
b=(B)oi.readObject();
}
catch(Exception e){}
System.out.println(b.x1+""+b.x2+""+b.x3);
}
}

class B implements Serializable{
static int x1=0;
volatile int x2=0;
transient int x3=0;
}
after running the output is x1=7,x2=7,x3=0
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
https://coderanch.com/t/546558/Streams/java/Serialization-static-variables
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic