Results 1 to 10 of 10
  1. #1
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default Transient variable

    I was asked on one of the interview about the transient variable,I knew few about it,because never used,but where can we use it,and why,for what purpose?
    Last edited by serjant; 11-09-2008 at 01:37 AM.

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    transient keyword avoid writing to Object persistence ( Object.serialize ) used possibly for security or perhaps if state of object should not be recovered across network connections.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default

    It has to do with keeping the value of a variable updated. The compiler/jvm can optimize code to not update a variable when the code implies that it would be. This would have implications in a multithreaded environment where a thread could update a variable and the compiler/jvm might not get around to storing it right away because (fill in the blank - say it sees it will be updated soon and figures to wait until them). Meanwhile another thread would see the old value.

  4. #4
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Aha,Thanks Nicholas and Norm,nice explanations.Helped much.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Sounds somewhat like a volatile variable, but I'm speaking out of ignorance as I've not used either.

  6. #6
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Exclamation latent genius

    Quote Originally Posted by Fubarable View Post
    Sounds somewhat like a volatile variable, but I'm speaking out of ignorance as I've not used either.
    I used them all the time, it was probably the first keyword after class that I really understood. I have established a defacto standard code that makes all variables

    private static transient volatile ..... and so on, unless there is some reason for an instance variable. Volatile has a nasty, very hidden failure mode that does not reveal in routine testing, I will try to recover my link to the discussion of that.

    ( ... i think fubarable is sandbagging here .... )

    Double checked locking is broken
    Last edited by Nicholas Jordan; 11-09-2008 at 01:00 PM.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  7. #7
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Nicholas ,where do use transient variables all the time?

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default

    @Furabale - you're right. I was talking about volatile. Did it off the top of my head without RTFM. Nick's description is right. I've never use either.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    No, not sandbagging. My programming knowledge is like my beard when I tried to grow one: very spotty.

  10. #10
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Smile transient keyword may not needed

    ( cannot see all of your reply )
    Nicholas ,where do use transient variables all the time?
    That is an involved answer, short version is that I do not always use 'transient' nor 'volatile' but I tend to put them in, it is sorta like your interview question - why does the interviewer ask the question? No short answer, actually. I tried to grasp Object persistence, basically I gave up and decided that if I wish to persist the state information, I will have to do some more reading. Until then, I will decide what if anything gets written to disk.

    Do not have the skills to explain myself fully - the basic issue would be defensive programming. We would have to examine at least in overview what you are programming for. There may be no need of 'transient' keyword for your application arena.

    Here's the first lines of the first class I wrote in Java:
    Java Code:
    /* Session invocation key.*/
    private transient volatile Pair key_ = null;
    private transient final java.lang.Integer MinPortNum = 0x0bb8; // 3,000
    private transient volatile java.awt.FileDialog fileDog = null;// "Arf, arf !" said fi
    //private transient volatile randomX.randomHotBits rhb = new randomX.randomHotBits();
    private transient static java.lang.String[] stopWords = {"a","about","above","across"
    private transient static java.util.Vector v = new java.util.Vector(stopWords.length);
    private transient static java.util.List processedGetterObjects;//
    Why do I code that way? Unless you are programming for a Networked Commercial arena, there may be difficulties in explaining why. There is a remarkably clear discussion of related matters in Marty Hall's Servlets book, it gives exact and full reasons why and why not.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. Demonstration of the transient keyword
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 12:09 AM
  2. transient keyword
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-11-2007, 11:27 AM

Posting Permissions

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