• Post Reply Bookmark Topic Watch Topic
  • New Topic

transient usage  RSS feed

 
Rr Kumaran
Ranch Hand
Posts: 548
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have below code snippet

class A implements java.io.Serializable
{
int a;
B b = new B(); // class B doesn't implement Serializable interface
}

now we know that class A's instance variable will be serialized and instance variable b will not be serializable. Now at this point what is the use of adding transient keyword to instance variable b ?


Thanks & Regards,
Kumar.
 
Joe Ess
Bartender
Posts: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you try to serialize A you will get a NotSerializableException. By declaring the instance of B as transient the serialization process will skip it and successfully serialize A.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!