Results 1 to 4 of 4
  1. #1
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default What are transient object

    Am working on GUIs and have come across a part that refers to transient objects.

    I dutifully sought a definition and came across, "a transient variable is a variable that may not be serialized."

    Needless to say, that was not helpful. Could someone just give me an overview of what this is all about or at least point me to a resource where I can get this information.

    Thanks in advance to all those who will respond.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    The Java Language Specification in 8.3.1.3 Transient Fields says:

    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:

    Java Code:
    class Point {
        int x, y;
        transient float rho, theta;
    }

    were saved to persistent storage by a system service, then only the fields x and y would be saved. This specification does not specify details of such services; see the specification of java.io.Serializable for an example of such a service.

    rho and theta are presumabely calculated somewhere, but transient marks a conscious decision not to serialise them. (They can always be calculated again when a serialised object is deserialised. Serialising would be redundant.)

    Another example is given in the javax.swing.ImageIcon class whose declaration begins:

    Java Code:
    public class ImageIcon implements Icon, Serializable, Accessible {
        /* Keep references to the filename and location so that 
         * alternate persistence schemes have the option to archive 
         * images symbolically rather than including the image data 
         * in the archive. 
         */
        transient private String filename; 
        transient private URL location; 
    
        transient Image image;
        transient int loadStatus = 0;

    The comment explains another reason why something might not be considered part of the persisted state: although the actual image is a substantial part of the state of an icon, when you persist the icon you might want to opt to only save the filename or url instead of all the bits that make up the image. Excluding these fields allows for choice at the time an instance is persisted.

    A third reason seems to apply to the loadStatus field: an icon might have loaded its image (so loadStatus is true) at the time it was persisted. But months later when it is unpersisted it may or may not have actually its image loaded. Again loadStatus is part of the state, but not part of the state that can be usefully persisted.

    Or consider a class with a timeForNextUpdate which holds a time in the future when some update should happen. When an instance is deserialised if it had serialised that field it might hold a bogus value in the past. Better not to serialise that field, but rather set a valid future date when the instance is deserialised.
    Last edited by pbrockway2; 02-05-2011 at 09:29 PM.

  3. #3
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Thank you for the speedy response. Your response, coupled with this explanation from stackoverflow have helped me understand.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    You're welcome. And thanks for the link.

    (And in case you haven't figured it out for yourself the answer to the question posed at the end of that thread - why is Hashable's Entry[] table transient? - is that it resembles rho and theta in the JLS example. The keys and values are serialised, so that it is easy enough to reconstruct that table when the instance is deserialised.)

Similar Threads

  1. transient variable
    By waqar100 in forum New To Java
    Replies: 3
    Last Post: 06-27-2010, 07:44 PM
  2. transient vs volatile
    By waqar100 in forum New To Java
    Replies: 2
    Last Post: 06-14-2010, 11:21 AM
  3. Transient & Local
    By Supamagier in forum Advanced Java
    Replies: 5
    Last Post: 04-28-2009, 03:35 AM
  4. Transient vraible
    By serjant in forum Advanced Java
    Replies: 9
    Last Post: 11-10-2008, 03:13 PM
  5. transient keyword
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-11-2007, 11:27 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •