Results 1 to 6 of 6
  1. #1
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default [SOLVED] Transient & Local

    Okay, I'm not sure if this belongs here or in 'New to Java', but anyway:

    As far as I know, a transient variable 'deletes' it's value after it's used locally - and can so be used over and over again. (I'm not sure if I'm right, correct me if I'm not).

    My question: What is the difference between local variables and (public) transient variables and what is the advantage from the one over the other?

    Thx.
    Last edited by Supamagier; 04-28-2009 at 11:17 AM. Reason: Solved
    I die a little on the inside...
    Every time I get shot.

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    transient variables cannot be serialized and local variables can. So in a sense you're right about them being "deleted" however in order to deserialize the rest of the object you must have a constructor that gives the transient variables some data.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  3. #3
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    The only effect of marking a field as transient is that it won't be serialized. Transient fields are not deleted, nor can they be "used locally". You cannot declare a local variable as transient.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  4. #4
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    Okay, thanks, but:
    Java Code:
    public class Test
    {
        private transient int var;
        void method() {
            var = 9;
            // operations with var
        }
    }
    Java Code:
    public class Test2
    {
        void method() {
            int var = 9;
            // operations with var
        }
    }
    In these 2 examples above, what would be the difference in, let's say, memory use/speed and such?

    Or has it something to do with safety?

    Tell me if I say something really stupid here ;)
    Last edited by Supamagier; 04-28-2009 at 02:07 AM.
    I die a little on the inside...
    Every time I get shot.

  5. #5
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    The previous posts have given you the answer, but without an explanation.

    The "transient" keyword applies only to instance variables, which are the ones defined outside of any method, like "var". Local variables are the ones defined inside a method.

    Local variables "go away" as soon as the ocde leaves the method or block they are defined in. Instance variables stay around as long as the instance of the class.

    "transient" does only one thing: It tells the compiler not to include the variable in the information saved when the instance is "serialized", which is a process of saving the instance's variables. Other than that, "transient" has no effect. I assume you are not doing serialization, so you are better off not using the "transient" keyword (it saves typing).

  6. #6
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    Okay, thanks :)
    I die a little on the inside...
    Every time I get shot.

Similar Threads

  1. Local IP
    By andrewms in forum Networking
    Replies: 5
    Last Post: 04-25-2009, 01:54 AM
  2. Transient & Volatile
    By Deepa in forum New To Java
    Replies: 2
    Last Post: 12-05-2008, 09:30 AM
  3. Transient vraible
    By serjant in forum Advanced Java
    Replies: 9
    Last Post: 11-10-2008, 02:13 PM
  4. Demonstration of the transient keyword
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:09 PM
  5. transient keyword
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-11-2007, 10: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
  •