• 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

why this works?

 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
why the following code works if it is said about the externalization that,
if we use exter. then we need to take care of the parent object as well on our own using getter methods for getting parameters or by direct access (if it's allowed) if the parent doesn't implement exter.
class a {
String n = "class a";
void f1() { S.o.p("a.f1()"); }
}
class b extends a implementes Externalizable {
String n = "class b";
void f2() { S.o.p("b.f2()"); }

public void writeExternal(ObjectOutput out) throws IOException {

out.writeObject(n);
}
public void readExternal(ObjectInput in ) throws IOException , ClassNotFoundException {

n = (String)in.readObject();
}
static void main(String[] s) {

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(new b());
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
b newB = (b)ois.readObject();
b.f1();
b.f2();
}
}
regards
maulin
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Maulin
What it gets serialized is the state, not the behaviour of the object. You are calling methods on the deserialized object, this is behaviour. The methods has not been serialized. You are only able to invoke them because the two classes a and b are already loaded in the program. Try deserializing this object in a program in which the classes are not in the classpath and you won't be able to send it any message.
There are several important points to remember when using Externalizable:
a)The no-arg constructor of the class of the object will be called when deserializing the object. This implies that the default constructor must exist, and must be public.
b)All the normal initialization is performed including the initialization at the point of the declaration.
c)Fields are not automatically de/serialized, but after steps a and b read/writeExternal are called.
New example

Output


a.f1()
Test.f2()
class b m o


Why the fields are de/serialized if no code is provided for it in read/writeExternal? They are not de/serialized. Because the constructor was called and the initialization at the point of the declaration took place fields have the default values desired by the programmer. If a modification to one of these fields is made in the object previous to serialization (as seeting o to "ooo") this change is not visible in the deserialized object because writeExternal didn't write any value in this example. In the ouyput the value for o is "o". Thus if you no provide proper read/writeExternal no state is serialized.
Bruce Eckel has examples about serialization in http://codeguru.earthweb.com/java/tij/tij0116.shtml
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi jose,
at last!! i got it!! i knew that behavior is not serialized but i forgot to put code accessing instance variables in the above code
but i tested it further and finally i am able to manage it. btw, i am not getting the exact reason why the constructor is called in externalization when we read the object?
i guess that is so because its better to do it that way as if we override readExternal() and writeExternal() and don't serialzied some of the fileds then while retreiving the object back we might get null values for the fields we have not written while serialzing the object. in serailization using Serializable it happens so as constructor is never called.
thanks a lot
maulin
 
Bring out your dead! Or a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic