A Simple Java Question.

james182

Member
Jun 7, 2003
48
0
0
How can you make a field serializable?
I know to declare a class to be serializable you say something like

import java.io.Serializable;
public class MyClass implements Serializable{

but when declaring a variable how can you say that that is also Serializable?

Thanks for any help you can provide.
-Kyle
 

mundane

Diamond Member
Jun 7, 2002
5,603
8
81
What kind of variables? I think that classes are either serializable, or they aren't. I'm not sure that you can declare them as such when you instantiate them.

However, if you wish to prevent a variable from becoming serialized, declare it Transient.

Dunno if this helps.

-Josh
 

chsh1ca

Golden Member
Feb 17, 2003
1,179
0
0
Serialization is simply taking the data that is in the object, and putting it into a string parsable form. The serialize() function returns this string, so that the data can be stored for later use. When re-instancing the object, you would use the unserialize() function, and pass the serial string as an argument. What you need to do in both functions is essentially take ALL the data in the class, and put it into a serial form. This means that if you have a bunch of booleans, chars, ints, etc., etc., you need to provide a way of converting it back and forth. You don't declare a variable as 'serializable', you would do that as the author of the serialize and unserialize function.
You can find more info here.