query about writeReplace and readResolve
I am trying to implement writeReplace and readResolve methods for a serializable class.
public class Ser implements Serializable
{
public Object writeReplace()
{
return new Ser();
}
}
above code works fine......write replace is called while serializing the object.
now i just change the return type of writeReplace method
public class Ser implements Serializable
{
public Ser writeReplace()
{
return new Ser();
}
}
now while serializing the object writeReplace is not called.
same happens for readResolve method.
I am not able to understand the reason since functions can not be differentaited base upon their return type.
can somebody explain ?
> I am not able to understand the reason since
> functions can not be differentaited base upon their
> return type.
Obviously they can, and anyway that's not what it says in the Javadoc http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getMethod(java.lang.String,%20java.lang.Class...)
or
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/reflect/Method.html#getReturnType()
The Serialization Specification defines the signature of the expected writeReplace()/readResolve() methods in #2.5:
http://java.sun.com/j2se/1.5.0/docs/guide/serialization/spec/output.html#5324
and #3.7:
http://java.sun.com/j2se/1.5.0/docs/guide/serialization/spec/input.html#5903
respecitvely. If you don't provide them, they aren't called.
ejpa at 2007-7-14 22:11:57 >

actually the documentation itself says that ->
If the method is defined, the writeReplace method is called to allow the object to designate its replacement in the stream. The object returned should be either of the same type as the object passed in or an object that when read and resolved will result in an object of a type that is compatible with all references to the object. If it is not, a ClassCastException will occur when the type mismatch is discovered.
So it shall not matter whether I have given Object as return type or the class object.
That statement refers to the fact that you don't have to actually return an object of the same class. You can do this:
class A
{
public Object writeReplace()
{
return new B();
}
}
class B
{
public Object readResolve()
{
return new A();
}
}
so A will be replaced by B in the stream, and B will replace itself by an A when deserialized.
The statement doesn't refer to the formal return type of the method, which must be Object in both cases. As you have already discovered.
Why would you want to change the return type from Object anyway? You aren't calling these methods yourself, so what's the point?
ejpa at 2007-7-14 22:11:57 >

ok let me ask it differently.......
since writeReplace is just a function...and is being called automatically in ObjectOutputStream...then how come function with return type as Object i.e
public Object writeReplace ()
{}
gets called whereas the function
public SomeCompatibleClass writeReplace()
{}
doesn't get called.
If there is a diffrence in call for these functions...then wat it is ? because I don't think while calling a function return type matters.