aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A query regarding serialization in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A query regarding serialization in Java" Watch "A query regarding serialization in Java" New topic
Author

A query regarding serialization in Java

Muthi ur Rahman Mohammed
Greenhorn

Joined: Mar 13, 2010
Posts: 2
Hi,

I am Muthi. I was learning about serialization and came across this code.

import java.io.*;

class Collar {
private int collarSize;

public Collar(int size) {
collarSize = size;
}

public int getCollarSize() {
return collarSize;
}

public void setCollarSize(int size) {
collarSize = size;
}
}

class Dog implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private transient Collar theCollar;
private int dogSize;

public Dog(Collar c, int size) {
theCollar = c;
dogSize = size;
}

private void writeObject(ObjectOutputStream oos) throws Exception {
try {
oos.defaultWriteObject();
oos.writeInt(theCollar.getCollarSize());
oos.close();
} catch (Exception e) {
//e.printStackTrace();
}
}

private void readObject(ObjectInputStream ois) throws Exception {
try {
ois.defaultReadObject();
theCollar = new Collar(ois.readInt());
ois.close();
} catch (Exception e) {
//e.printStackTrace();
}
}

public Collar getCollar() {
return theCollar;
}

public int getDogSize() {
return dogSize;
}

public void setDogSize(int size) {
dogSize = size;
}

}

public class serializeDog {
public static void main(String[] args) {

Collar c = new Collar(6);
Dog d = new Dog(c, 7);
System.out.println("** Dog Size :" + d.getDogSize() + ", Collar Size :"
+ c.getCollarSize());

try {
FileOutputStream bbb = new FileOutputStream("objects.txt");
ObjectOutputStream oos = new ObjectOutputStream(bbb);
oos.writeObject(d);
//oos.close();
} catch (Exception ex) {
//ex.printStackTrace();
}
d.setDogSize(23);
c.setCollarSize(99);
System.out.println("Serialization Done!");
System.out.println("Dog " + d.getDogSize() + ", collar :"
+ c.getCollarSize());

try {
FileInputStream aaa = new FileInputStream("objects.txt");
ObjectInputStream ois = new ObjectInputStream(aaa);
d = (Dog) ois.readObject();
//ois.close();
} catch (Exception ex) {
//ex.printStackTrace();
}
System.out.println("deSerialization Done!");
System.out.println("**** Dog Size :" + d.getDogSize()
+ ", Collar Size :" + d.getCollar().getCollarSize());
}
}

OUTPUT:

** Dog Size :7, Collar Size :6
Serialization Done!
Dog 23, collar :99
deSerialization Done!
**** Dog Size :23, Collar Size :99

As you can see in the code initially the collar size is 6 and dog size is 7 follwing which serialization is done. The values of collar size and dog size are then changed to 99 and 23 respectively. After the values are changed, deserialization is being done. When you look at the output you see the values of collar size and dog size are 99 and 23 but when the object was serialized it contained values 6 and 7. How and why is this happening?
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

Please remove the comments in catch blocks and rerun the code. You will get the answer
And please use code tag to post the code

Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Please QuoteYourSources and remember that serialization is no longer on the exam.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3051
    
  36

Howdy Muthi, welcome to JavaRanch.

Please UseCodeTags when you post a code. It's unnecessarily hard to read the code otherwise.
Please edit your post to add code tags by clicking the button.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
KrishnaPrasad raghavan
Ranch Hand

Joined: Oct 28, 2008
Posts: 46
Wouter Oet said "Please QuoteYourSources and remember that serialization is no longer on the exam."


I think serialization is very much there for the exam.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

KrishnaPrasad raghavan wrote:I think serialization is very much there for the exam.

Serialization is not on the SCJP objectives now, so you don't need to study it for the purpose of the exam...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

Serialization is not a part of Java 1.6 exam
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A query regarding serialization in Java