aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes transient variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "transient variables" Watch "transient variables" New topic
Author

transient variables

Chuanshuang Bie
Greenhorn

Joined: May 19, 2000
Posts: 11
Please examine the following 2 codes:
1)
class Test {
transient int maxElements = 100;
public static void main(String []args){
Test t= new Test();
System.out.println(t.maxElements);
}
}
100 is printed. Why?
2)
class Test {
//transient int maxElements = 100;
public static void main(String []args){
transient int maxElements = 100;
Test t= new Test();
System.out.println(t.maxElements);
}
}
compile error. Any one has any idea? Thanks.
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Perhaps... local variables may not be declared as transient. Makes sense right? You would only serialize the data, not the methods.
Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
jayashree murugan
Greenhorn

Joined: Feb 10, 2000
Posts: 22
Makes perfect sense!
Chuanshuang Bie
Greenhorn

Joined: May 19, 2000
Posts: 11
Perhaps methods may not be defined as: transient void aMethod(){}; but that's about variables in a method not method itself.
For my first question, why a transient variable gets printed out?
I use jdk1.2.2 on PC. Anything wrong? Thanks.
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
My friend,
class variables ie., the variables you define in the class are different than the ones that you define for a method. The method variables are shortlived, only for the duration of the method where as the class variables have the same lifetime as the object that contains them. And such local variables DO NOT represent the state of the object.
The class variables are accessible to all the methods defined in the class. Ofcourse there are some nitty-gritty rules about being static/nonstatic etc. In your first example, it was perfectly valid to access the static class variable from the static main method. The keyword transient has no effect, what so ever, on the accesibility.
In your second example, there is no such variable defined that the println is trying to access!!.
Class holds data and methods represents operations on that data. An Object is (usually) a fully contained entity. Serialization means persisting( making permanent ) the state of the object. State applies to the data held by the class which means there is no such thing as "persisting the methods". That's why only variables can have the transient qualifier and not methods.
Is this explanation helpful? Am I sounding theoretical??
Ajith

[This message has been edited by Ajith Kallambella (edited May 19, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
The transient does not affect the usage of the variables
in your code. The transient keyword marks that the variables
will not be serialized when you serialized your object.
So transient is only useful if you are doing a lot
of Serialization.
Chuanshuang Bie
Greenhorn

Joined: May 19, 2000
Posts: 11
Thank you all, Ajith & Wirianto. I got it now. My second example will be ... maxElements not t.maxElements. sorry about that. Thank you again.
Tom P
Ranch Hand

Joined: May 06, 2000
Posts: 88
Method variables can not be defined as transient or static. Why? Static: Because static variables are shared by all instances of an object but method variables exist only while a method is running. Transient: because method variables are never serialized. They exist only while a method is running and the serialized object does not represent the object's state in the middle of a method.
As to the first code sample: why wouldn't 100 print? Transient variables exist just like any other variable... the only difference is they won't be serialized if the object is serialized.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: transient variables
 
Similar Threads
Equals
static final transient int i=100??
Where should we write static final transient
overriding equals
Miscellenous Code for Transient Variable