Yes, you will get an exception when the ObjectOutputStream attempts to write
that member variable. When the class is Serializable, it assumes that any
fields (members variables) that are not transient or static are also
Serializable.
It's perfectly legal to have a Serializable class like this:
public class Foo implements Serializable {
private Object bar;
}
The stipulation is that whatever bar references, at the time the foo
instance is serialized, be a serializable class also. If the instance
referenced by the 'bar' member variable was of type UserDefined.class, for
example, and the UserDefined class is not serializable, a
NotSerializableException will be thrown telling you that the class
com.acme.UserDefined is not serializable.
There are three ways to have non-serializable references on your
Serializable class.
1. Declare the member variable static
2. Declare the member variable transient
3. Add the serialPersistentFields member variable to your class
The serialPersistentFields member variable tells the ObjectOutputStream
which member variables it should serialize. You use it like this:
public class Foo implements Serializable {
private Object bar;
private MyClass myInstanceRef;
private static final ObjectStreamField[] serialPersistentFields
= {new ObjectStreamField("myInstanceRef", MyClass.class)};
}
Alternatively, you can declare your class Foo as Externalizable and
implement readExternal and writeExternal, then you can simply write the data
you know needs to be written. This will be faster too as it involves less
reflection and less caching.
Good luck!
David Blevins
---
OpenEJB - EJB Container System
www.opene.org/openejb
> -----Original Message-----
> From: A mailing list for Enterprise JavaBeans development
> [mailto:[EMAIL PROTECTED]]On Behalf Of Ashwani Kalra
> Sent: Friday, December 28, 2001 8:01 AM
> To: [EMAIL PROTECTED]
> Subject: Serialization problem
>
>
> Hi,
> Here is what I am doing
> One of the remote fn of the SSB returns a object which is
> serializable but
> it contains a member variable class(User Defined) which is not
> serializable
> and I dont need it to be serializable then will there be any problem like
> runtime exception , or system exception when container will return this
> class, if I dont declare that variable as transient.?
>
>
> Environment:
> App server:weblogic 6.0 on linux
> webserver :Apache on solaris.
>
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Regds
> Ashwani Kalra
> Sr. Mem. Dev. Staff
> Aithent Technologies
> India
> http://www.geocities.com/ashwani_kalra/
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> ==================================================================
> =========
> To unsubscribe, send email to [EMAIL PROTECTED] and include
> in the body
> of the message "signoff EJB-INTEREST". For general help, send email to
> [EMAIL PROTECTED] and include in the body of the message "help".
>
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".