Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

static final variable and serialization

Thread view: 
swengineer001@gmail.com - 27 May 2007 18:23 GMT
I have a design that has a collection of serializable objects which
contain several static final variables. I am wondering what will
happen with these if I were to output the collection to a file. I am
just learning Java but have several years of embedded experience with
C. In C I would have had these variables #defined in a header and not
written them to the file.

Thanks for any insight
Andrew Thompson - 27 May 2007 18:31 GMT
>I have a design that has a collection of serializable objects which
>contain several static final variables. I am wondering what will
>happen with these if I were to output the collection to a file.

Why wonder, rather than test?  (Feel free to report the results)

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Daniel Pitts - 27 May 2007 18:36 GMT
On May 27, 10:23 am, "swengineer...@gmail.com"
<swengineer...@gmail.com> wrote:
> I have a design that has a collection of serializable objects which
> contain several static final variables. I am wondering what will
[quoted text clipped - 4 lines]
>
> Thanks for any insight

I believe serializing works on the instance level.  You could (as
Andrew Thompson suggests) write a test a report your findings for
future programmers.
Tom Hawtin - 27 May 2007 19:40 GMT
> I have a design that has a collection of serializable objects which
> contain several static final variables. I am wondering what will
> happen with these if I were to output the collection to a file. I am
> just learning Java but have several years of embedded experience with
> C. In C I would have had these variables #defined in a header and not
> written them to the file.

Only instance data is serialised. If instances have references to the
objects pointed to be the static variables, those objects will be
serialised to. However, there is no record that static variables pointed
to those instances. If they were, what would you expect to happen to
those static final variables when you deserialised?

In general, avoid mutable static data like the plague (even if labeled
'singleton' to pretend that it isn't a bodge).

Tom Hawtin
Twisted - 27 May 2007 21:48 GMT
> swengineer...@gmail.com wrote:
> > I have a design that has a collection of serializable objects which
[quoted text clipped - 14 lines]
>
> Tom Hawtin

Of course, you could end up with this situation:
Static -> object
Instance -> object
before serialization,
Static -> object
Instance -> object copy
after.
Previously, the static variable reference == the instance's;
subsequently, they are no longer ==, though probably equals() if that
isn't the default object equals().

If that matters, you have a problem. The solution is then to have a
singleton and snap the references with readResolve -- of course, this
is probably evil if it's mutable, but I've had occasion to want an
immutable singleton, such as

class FooValue {
private static FooValue sentinel = SpecialCaseFooValue.instance;
...
}
class SpecialCaseFooValue extends FooValue {
private static FooValue instance = new SpecialCaseFooValue();
private SpecialCaseFooValue () { ... }
private Object readResolve () { return instance; }
...
}

It's then possible to compare FooValues against FooValue.sentinel
efficiently with == and never get a false negative, including after
data structures containing sentinel have been serialized and
deserialized. (At least, if you don't do anything weird/fancy with
class loaders, just straight Java programming.)

This comes in handy if you want to make e.g. a bigdecimal type with
inf and nan values, or a unique instance of zero. Also for various
data structures where you want to use a sentinel value somewhere but
null is unsuitable. Generally where you want an immutable object that
isn't wastefully duplicated and may be efficiently tested for.

The primary non-evil use of a mutable singleton, for some kind of
caching or logging interface, is also best treated similarly. All of
its fields should be transient, so it doesn't take up wasteful amounts
of space when serialized, and it should have a readResolve to coerce
references to the current runtime's instance on read. That's if you
can't avoid having references to the cache, logger, or whatever in
serializable objects. If you can, don't even make it serializable.
(This is quite unlike the special/sentinel value case, which probably
must be serializable as it likely occurs in data structures or
numerical stuff that needs to be serialized without bits going
missing.)
Twisted - 27 May 2007 21:53 GMT
> class SpecialCaseFooValue extends FooValue {
> private static FooValue instance = new SpecialCaseFooValue();
> private SpecialCaseFooValue () { ... }
> private Object readResolve () { return instance; }
> ...

Bah -- that should be

class SpecialCaseFooValue extends FooValue {
public static FooValue instance = new SpecialCaseFooValue();
private SpecialCaseFooValue () { ... }
private Object readResolve () { return instance; }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.