This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin Cookbook and have Ken Kousen on-line!
See this thread for details.
Win a copy of Kotlin Cookbook this week in the Kotlin 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

A query regarding serialization in Java

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please QuoteYourSources and remember that serialization is no longer on the exam.
 
Sheriff
Posts: 6447
1156
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 9660
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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...
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serialization is not a part of Java 1.6 exam
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!