Skip to Content Java Solaris Communities Partners My Sun Sun Store United States Worldwide

»  Spotlight Articles
»  Projects
»  Publications
»  People
»  Awards
»  Events
»  Downloads
»  Internships
»  Contrarian Minds
»  About Sun Labs

OPJ Tutorial: Problems with the transient modifier

The Java language provides the transient modifier although, in the initial version of the language specification (JLS), the semantics are only partly specified. Quoting the JLS:

"Variables may be marked transient to indicate that they are not part of the persistent state of an object. If an instance of the class Point:

  class Point {
    int x, y;
    transient float rho, theta;
  }
were saved to storage by a system service, then only the fields x and y would be saved. This specification does not yet specify details of such services; we intend to provide them in a future version of this specification."

The OPJ specification interprets the transient modifier in the definition of persistence reachable. An object is persistence reachable if it is reachable by the standard JLS rules, unless it is reachable solely through chains of reference chains that pass through any variables marked with the transient modifier.

Unfortunately, since JDK 1.1, the transient modifier has been given a specification that is specific to Java Object Serialization, and is incompatible with the natural interpretation for orthogonal persistence. The specification is also incompatible with the ODMG Java binding. The essence of the incompatibility is an idiomatic use of transient to indicate a field that will be made persistent (serialized) but will have its representation transformed by user-code as part of the serialization process. The classic example is the java.util.Hashtable class, where the table entries themselves are marked transient.

The OPJ specification contains a proposed language change that adds sub-modifiers to the transient modifier, to distinguish object serialization and object persistence. Until this is accepted and implemented, the specification chooses to ignore the transient modifer on non-static variables and defines an API mechanism for indicating that a field is transient for object persistence purposes, namely OPTransient.mark("fieldName"). For example, here is a fragment of a class that manages an open file, in which the fd variable represents external state that is associated with the process supporting the OPJ Virtual Machine, and therefore should not be made persistent.

public class FileStream {     private transient FileDescriptor fd;     static { OPTransient.mark("fd"); } }

The method OPTransient.mark must be called in a static initializer of the class defining the field passed as argument.

Note that, since static variables are ignored by object serialization, OPJ does interpret  "static transient". It is harmless but redundant to also call OPTransient.mark on a static transient field.

PREV: Storage ManagementNEXT: Using AWT and Swing with OPJ, Tutorial Home Page


Last changed: Dec 7th 1999

Questions and comments to forest-info@sunlabs.com
Would you recommend this Sun site to a friend or colleague?
Contact About Sun News Employment Privacy Terms of Use Trademarks Copyright 1994-2008 Sun Microsystems, Inc.