Results 1 to 6 of 6
Thread: Transient & Local
- 04-27-2009, 08:02 PM #1
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
[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.
- 04-27-2009, 08:07 PM #2
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.
- 04-27-2009, 09:59 PM #3
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
- 04-28-2009, 01:55 AM #4
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
Okay, thanks, but:
Java Code:public class Test { private transient int var; void method() { var = 9; // operations with var } }In these 2 examples above, what would be the difference in, let's say, memory use/speed and such?Java Code:public class Test2 { void method() { int var = 9; // operations with var } }
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.
- 04-28-2009, 02:21 AM #5
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).
- 04-28-2009, 02:35 AM #6
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
Similar Threads
-
Local IP
By andrewms in forum NetworkingReplies: 5Last Post: 04-25-2009, 01:54 AM -
Transient & Volatile
By Deepa in forum New To JavaReplies: 2Last Post: 12-05-2008, 09:30 AM -
Transient vraible
By serjant in forum Advanced JavaReplies: 9Last Post: 11-10-2008, 02:13 PM -
Demonstration of the transient keyword
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 11:09 PM -
transient keyword
By Java Tip in forum Java TipReplies: 0Last Post: 12-11-2007, 10:27 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks