• 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

serialization question - pls help me!

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont understand the effect of serialization. What can it actually contribute to the following objects and code?

i just cant see it!

i tried to test the effect by altering the code but dont know where to start...coz i am not quite sure (still) what serialization is ALL about!

K&B added in order to alter the standard deserialization process you would override the readObject() method in SpecialSerial. Why???

and most importantly, if serialization is about saving objects (instance variables), line 9's ++s.z is outside os.close(),
how come on line 14, the value changed?

====here is the code======
import java.io.*;
public class TestSer {
public static void main(String[] args) {
SpecialSerial s = new SpecialSerial();
try {
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("myFile"));
os.writeObject(s);
os.close();
System.out.println(++s.z + " "); //line 9

ObjectInputStream is = new ObjectInputStream(new FileInputStream("myFile"));
SpecialSerial s2 = (SpecialSerial)is.readObject();
is.close();
System.out.println(s2.y + " " + s2.z); //line 14
} catch (Exception x)
{System.out.println("exc"); }
}
}

class SpecialSerial implements Serializable {
transient int y = 7;
static int z = 9;
void readObject(){ };
}
[ September 17, 2007: Message edited by: adam lui ]
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adam,
Please dont mind , but you need to do your homework here.

1. its "private void readObject(Objectlnputstream os) {} " that is to be overridden and NOT readObject(); // No Params?!
2. Serialization is NOT for Transients. All transients will be given Default value of its datatype when object is deserialized.
3. Serialization is NOT for Statics. All Static variables, even though when referred thru Object Reference, fetches its value from Class (and not Object).

Please let me know if I can still help.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. No readObject(...) Method can be overriden, the method must be implemented in any class which needs special treatment in the deserialization process.

2. Static members should be accessed in a way like SpecialSerial.z and then it's very easy to see why static member vars are not serialized.

[ September 19, 2007: Message edited by: Roland Schenner ]
[ September 19, 2007: Message edited by: Roland Schenner ]
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please refer below link. You will clear.
here
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy "Ajay C" !

Thanks for your contribution to this forum.

The Java Ranch follows a certain policy regarding user names.
The main reasons why and a link how to change yours you'll find here:
http://www.javaranch.com/name.jsp


So, could you please change your user name before your next posting?
It will not affect anything you've already posted here. Just your user name will update.


I'm posting this because I am one of the moderators of this forum.


Yours,
Bu.
reply
    Bookmark Topic Watch Topic
  • New Topic