Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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

Object serialization

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 66 in http://www.angelfire.com/or/abhilash/Main.html:
Class fields with the following modifiers will not be serialized

1)private
2)static
3)transient
4)protected
Why is the answer is 2 and 3? I thought it is just 3.
Thanks.

------------------
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static variables are not serialized. Transient allows you to mark instance variables to be non serializable. Its the rules. When you serialize something, it contains information about the instance only. No information is stored about the class.
 
quan zhu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But look at following code:
import java.io.*;
public class TransientWriter implements Serializable
{
public static String s = "static String! ";
public String s2="this is a string! ";
public transient String s3="transient";

public String toString()
{
return s+s2+s3;
}
public static void main(String args[]) throws IOException, ClassNotFoundException
{
TransientWriter tw = new TransientWriter();
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("tw.out"));
out.writeObject(tw);
ObjectInputStream in = new ObjectInputStream(new FileInputStream("tw.out"));
TransientWriter tw2 = (TransientWriter) in.readObject();
System.out.println(tw2);
}
}
why is the output is "static String! this is a string! null"?
Holly

Originally posted by Sam Wong:
static variables are not serialized. Transient allows you to mark instance variables to be non serializable. Its the rules. When you serialize something, it contains information about the instance only. No information is stored about the class.


 
Sam Wong
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the class has been loaded with static field s = "static String! ". When you read the serialized object and call toString(), it read s2. s3 is transient so it wasn't serialized. s is static and is in memory already. So that's why it looks like it got serialized but it didn't.
 
Sam Wong
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To confirm this for yourself, insert the following line just before you read the serialized object.
TransientWriter.s = "Hello ";
If the static field s got serialized, then the call toString() will print "static String! this is a string! null" instead of "Hello this is a string! null".
Give it a try.
 
quan zhu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you a lot!
reply
    Bookmark Topic Watch Topic
  • New Topic