File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why this works? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why this works?" Watch "why this works?" New topic

why this works?

Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
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 {

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();
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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


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

SCJP2. Please Indent your code using UBB Code
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
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
I agree. Here's the link:
subject: why this works?