• 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

Question about Serialization

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Following is the mock question at the end of chapter 6 from K & B Book.


import java.io.*;
public class TestSer {
public static void main(String[] args) {
SpecialSerial s = new SpecialSerial();
try {
ObjectOutputStream os = new ObjectOutputStream(
new FileOutputStream("myFile"));
os.writeObject(s); os.close();
System.out.print(++s.z + " ");
ObjectInputStream is = new ObjectInputStream(
new FileInputStream("myFile"));
SpecialSerial s2 = (SpecialSerial)is.readObject();
is.close();
System.out.println(s2.y + " " + s2.z);
} catch (Exception x) {System.out.println("exc"); }
}
}
class SpecialSerial implements Serializable {
transient int y = 7;
static int z = 9;
}



Which are true? (Choose all that apply.)
A. Compilation fails.
B. The output is 10 0 9
C. The output is 10 0 10
D. The output is 10 7 9
E. The output is 10 7 10
F. In order to alter the standard deserialization process you would override the readObject()
method in SpecialSerial.
G. In order to alter the standard deserialization process you would override the defaultReadObject() method in SpecialSerial

Answer is
C and F are correct. C is correct because static and transient variables are not serialized when an object is serialized. F is a valid statement.

I am confused if C is correct then why F is also correct please help
 
author
Posts: 23949
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Answer is
C and F are correct. C is correct because static and transient variables are not serialized when an object is serialized. F is a valid statement.

I am confused if C is correct then why F is also correct please help



How about wording it like this? ... The output is 10 0 10, because in the standard serialization process, static and transient variables are not serialized when an object is serialized. However, if you would like to alter the standard serialization process to serialize static and transient variables, you would override the readObject() method in the SpecialSerial class.

Does this statement include both C and F, and still make sense?

Henry
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry, you said perfectly correct, Serialization is not for statics! and whenever, deserialization is done, transients are given their default values not initialized values.
In order to write and read something extra during serialization, to save it with object's state and retreive it! we override writeObject(ObjectOutputStream os) and readObject(ObjectInputStream ois), and we invoke defaultWriteObject() and defaultReadObject() to tell the JVM to do normal serialization process. But what happens if i do not invoke these methods, and directly write an int say writeInt(i) and then readInt(i).
Actually, i have my exam next weekend, and i would really appriciate the answer.
 
Skool. Stay in. Smartness. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic