Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How should I declare fields within my serializable class to prevent them from being serialized?

0
10 Posted

How should I declare fields within my serializable class to prevent them from being serialized?

0
10

Location: http://www.jguru.com/faq/view.jsp?EID=1197 Created: Nov 22, 1999 Modified: 1999-12-22 23:55:48.555 Author: Govind Seshadri (http://www.jguru.com/guru/viewbio.jsp?EID=14) Within JDK 1.1, any non-static field declared as transient is not serialized. (Static fields are not serialized anyway) Java 2 provides you with another mechanism for specifying which fields need to be serialized. Your class can declare the static field: public final ObjectStreamField[] serialPersistentFields = {…}; initialized with instances of ObjectStreamField, within each instance indicating the name and type of the serializable field. For example: public class FooBar implements Serialzable { private Foo foo; private int bar; private String passwd; public final static ObjectStreamField[] serialPersistentFields = { new ObjectStreamField(“foo”,Foo.class), new ObjectStreamField(“bar”,Integer.TYPE) }; } indicates that only the fields foo and bar must be serialized.

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123