• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

transient variables

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps... local variables may not be declared as transient. Makes sense right? You would only serialize the data, not the methods.
Ajith
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Makes perfect sense!
 
Chuanshuang Bie
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all, Ajith & Wirianto. I got it now. My second example will be ... maxElements not t.maxElements. sorry about that. Thank you again.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic