views:

123

answers:

2

I was using the Mersenne-Twister implementation at http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/VERSIONS/JAVA/MTRandom.java as a drop-in replacement for the default java.util.Random class. However, four fields (an int, a boolean and two byte[]) are marked as transient. This means that I can't serialize an object of this class without implementing custom functionality.

The question is, is there any reason that these fields are marked transient? Is there any code in there that holds information that won't make any sense when the object is read in from a file? I removed the transient modifier from the fields and it seems to work fine, but I haven't tested it intensively and so might there be cases where it breaks?

Personally, I can't see why, since all that's done in the class is arithmetic.

A: 

Most likely the reasoning behind making all of the non-static fields of the class transient was so that the MTRandom class stays binary compatible with java.util.Random, from which it is extended.

So theoretically, you could serialize an MTRandom instance, and deserialize it as a Random instance and everything would work.

If those fields aren't transient, then they would be serialized and become incompatible.

However, as far as I can tell, removing the transients shouldn't cause a problem for you.

karoberts
It does not remain "binary compatible". Java serialisation does bit work like that.
Tom Hawtin - tackline
+1  A: 

From the comment on serialVersionUID, it looks like the author didn't want to consider serialisation. Adding transient may have suppressed some compiler/IDE warnings.

Tom Hawtin - tackline