This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubts in Serialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubts in Serialization" Watch "Doubts in Serialization" New topic
Author

Doubts in Serialization

shashi booshan
Greenhorn

Joined: Jun 13, 2008
Posts: 14
Hi guys in the following im gettin problem with Dog () and the Collar().Not gettin it properly.can somebody help me ?




import java.io.*
public class SerializeDog {
public static void main(String[] args)
{
Collar c = new Collar(3);
Dog d = new Dog(c,7);
System.out.println("Before serialization the collar size is :"+ getCollar() + d.getCollarSize());

try {
FileOutputStream fos = new FileOutputStream("myfile.txt");
ObjectOutputStream os = new ObjectOutputStream (fos);
os.writeObject(d);

os.close();
}
catch(Exception e) {
e.printStackTrace();
}

try {
FileInputStream fis = new FileInputStream("myfile.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
d = (Dog) ois.readObject();
ois.close();
}
catch(Exception e) {
e.printStackTrace();
}
System.out.println("Before serialization the collar size is :"+ d.getCollar + d.getCollarSize());
}
}
Class Dog implements Serializable {
private Collar theCollar;
private int dogSize;
public Dog(Collar collar,int size) {
theCollar = collar;
dogSize = size;
}

public Collar getCollar() {
return theCollar;
}

Class Collar implements Serializable {
private int collarSize;
public Collar(int size) {
collarSize = size;
}
public getCollarSize();
return collarSize;
}

]


Thanks & Regards
- Shashi
kamal shah
Greenhorn

Joined: Feb 26, 2008
Posts: 18
<blockquote>code:
<pre name="code" class="core"> import java.io.*;
public class SerializeDog {
public static void main(String[] args){
Collar c = new Collar(3);
Dog d = new Dog(c,7);
System.out.println("Before serialization the collar size is :"+ (d.getCollar()).getCollarSize());
System.out.println("Collor referece is ::"+d.getCollar());
System.out.println("Dog referece is ::"+d);
try {
FileOutputStream fos = new FileOutputStream("myfile.txt");
ObjectOutputStream os = new ObjectOutputStream (fos);
os.writeObject(d);

os.close();
}catch(Exception e) {
e.printStackTrace();
}

try {
FileInputStream fis = new FileInputStream("myfile.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
d = (Dog) ois.readObject();
ois.close();
}catch(Exception e) {
e.printStackTrace();
}

System.out.println("After deserialization the collar size is :"+ (d.getCollar()).getCollarSize());
System.out.println("Collor referece is ::"+d.getCollar());
System.out.println("Dog referece is ::"+d);
}
}

class Dog implements Serializable {
private Collar theCollar;
private int dogSize;

public Dog(Collar collar,int size) {
theCollar = collar;
dogSize = size;
}
public Collar getCollar() {
return theCollar;
}
}

class Collar implements Serializable {
private int collarSize;

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

public int getCollarSize(){
return collarSize;
}
}
</pre>
</blockquote>

Shashi Run the above code,And watch the dog and collar references values.It will surely clear your doubt.


Regards,<br />Kamal<br /> <br />Life is nothing but a competition to be the criminal rather than the victim -Bertrand Russell
 
wood burning stoves
 
subject: Doubts in Serialization
 
Similar Threads
Serialization
A query regarding serialization in Java
Getting an exception(Serialization)
Serialization JSCP 6.0 (manually invoking writeObject() )
Serialization