Results 1 to 9 of 9
Thread: Serialization query
- 01-23-2009, 02:01 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 30
- Rep Power
- 0
Serialization query
Hi,
I have a class A that is serializable. This particular class contains three Strings and an instance of class B as its member variables (class B is not serializable). When i will try to serialize class A, it will throw an java.io.NotSerializableException because class B is not serializable. I can't even place the modifier 'transient' before the instance of the class B. Any ideas, how to go with this situation ?
- 01-23-2009, 04:00 PM #2
Look at the Serializable interface API. It contains methods you can override. In those methods, you would handle serializing each field that can be serialized. I *think* this is just a matter of writing each of the fields to the object output stream or reading them back in from it.
- 01-24-2009, 09:02 AM #3
Member
- Join Date
- Dec 2008
- Posts
- 30
- Rep Power
- 0
Had it been allowed to place the 'transient' identifier before the class B's instance, it would have been great. Why is it not allowed ? If you see you can place the 'transient' identifier before the instances of String class and make it escape the process of serialization. Then why not before our own classes :(
- 01-24-2009, 08:54 PM #4
You're right, the transient keyword should fix the situation. You shamed me into opening Eclipse and creating a test.
As is, the code will run but cause a not serializable exception, because I commented out the transient keyword. Remove the /*, */ and the exception goes away.Java Code:package test; import java.io.ByteArrayOutputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class SerialTest { public static void main(String[] args) { SerialTest instance = new SerialTest(); instance.run(); } public void run() { ClassA classA = null; final ObjectOutputStream oos; classA = new ClassA(); try { oos = new ObjectOutputStream(new ByteArrayOutputStream()); oos.writeObject(classA); } catch (Throwable t) { t.printStackTrace(); } } } class ClassA implements Serializable { /** For serialization, stops compiler warning */ private static final long serialVersionUID = 1L; private String _String = "Initial value"; private [B][COLOR="Red"]/*transient*/[/COLOR][/B] ClassB _ClassB = new ClassB("First value"); public void setString(String string) { _String = string; } public String getString() { return _String; } public void setClassB(ClassB classB) { _ClassB = classB; } public ClassB getClassB() { return _ClassB; } } class ClassB { private final String _FinalString; public ClassB(final String pString) { _FinalString = pString; } public String getFinalString() { return _FinalString; } }
Copy and paste the above and make sure it runs for you...
- 01-25-2009, 07:14 AM #5
Member
- Join Date
- Dec 2008
- Posts
- 30
- Rep Power
- 0
Your code definitley worked and the idea of placing the 'transient' identifier was successfull. But somehow, placing the 'transient' identifier in my code gives compilation error. I am explaining my code:
One serializable class:
package com.test;
import java.io.Serializable;
public class SerializableClass implements Serializable {
static final long serialVersionUID = 1L;
String str1 = "Sun";
String str2 = "MicroSystems";
}
One Non-serializable class:
package com.test;
public class NonSerializableClass {
String one = "one";
String two = "two";
}
The main class:
package com.test;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class Serl {
public static void main(String[] args) {
SerializableClass serializableClass = new SerializableClass();
/*transient*/ NonSerializableClass nonSerializableClass = new NonSerializableClass();
FileOutputStream fos = null;
ObjectOutputStream out = null;
try {
fos = new FileOutputStream("D:\\Test\\MyFile");
out = new ObjectOutputStream(fos);
out.writeObject(serializableClass);
out.writeObject(nonSerializableClass);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Placing the 'transient' identifier before the NonSerializableClass instance gives a compilation error. Any ideas Sir ?
- 01-26-2009, 12:43 AM #6
OK, I saw some things.
If you look at my example, ClassA contains an instance of ClassB. The main class, SerialTest, serializes ClassA.
*Perhaps* a class can serialize itself, and I can see situations where that would make sense. But a more typical approach would be to have a main logic class that serializes other classes and does something with the stream.
Your example serializes both ClassA and ClassB, and ClassA does not contain a reference to ClassB. Therefore, the first writeObject() should work, where the second one can't, because ClassB is not serializable.
Look again at my example, at the structure of the three classes. I based it on your original description.
--
In your example, you defined your two fields as *local* variables in the main() method. That is why you are getting the error. Only instance variables can be marked transient.
--
I've gone through this frustration before, where my lack of understanding of the technology leads me to make basic mistakes in the structure of my classes.
Go back and think through what you want to accomplish, and how the class design gets the job done. Then worry about the transient keyword.
- 01-26-2009, 06:40 AM #7
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
Why are you trying to serialize a nonserializable object? This isn't going to work:
Java Code:out.writeObject(nonSerializableClass);
- 01-27-2009, 06:19 AM #8
Member
- Join Date
- Dec 2008
- Posts
- 30
- Rep Power
- 0
Steve - I got the point. I pasted the code which is somewhat (rather quite) different from the problem I mentioned. None the less, I got the point that in both the cases I was trying to place the "transient" keyword before the *local* variable which is not allowed. In both the cases placing the "transient" keyword before the instance variable works fine. Really had a nice time on this thread.
- 01-27-2009, 03:04 PM #9
Similar Threads
-
Java serialization
By paul in forum Advanced JavaReplies: 3Last Post: 04-10-2009, 08:58 PM -
about serialization
By bishnu in forum New To JavaReplies: 0Last Post: 12-19-2008, 09:13 AM -
Serialization/Deserialization Error
By andrepezzo in forum NetworkingReplies: 0Last Post: 12-16-2008, 04:21 PM -
Need help using serialization
By xcallmejudasx in forum New To JavaReplies: 0Last Post: 12-02-2008, 08:23 PM -
What is Serialization and de-serialization in Java
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks