This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Serialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Serialization" Watch "Serialization" New topic
Author

Serialization

sonia jaswal
Ranch Hand

Joined: Jun 01, 2007
Posts: 42
code:


class Dog implements Serializable {
private int dogSize;
public Dog(int size)
{
dogSize = size;
}
}
class Collar
{
private int collarSize;

public Collar(int size) { collarSize = size; }
public int getCollarSize() { return collarSize; }
}
public class SerializeDog {
public static void main(String [] args) {
Dog d = new Dog(5);}



// here the Collar class is non serializable, and its private instance variables are not being used by the serializable dog object... and there is no inheritance also and nothing has been declared transient... so after de-serialization the Collar class variables will be reinitialized to their default values.... is this right.. or will there be a compilation error...(as the non serializable class has no link with the serialized object dog and there in no inheritance also)
There is no HAS-A relationship between dog and collar... nor there is a IS-A relationship...
Will it still give a compilation error... or will it run...???
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38036
    
  22
The answer is,
.
.
.
.
.
.
.
NO.

Try it and see what actually happens.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38036
    
  22
Actually I think I may be mistaken; anyway try it.

You have confused me by introducing a Collar class. Try enhancing the Collar class by adding this line to its constructor:
throw new RuntimeException();
See what happens . . . and what doesn't happen.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38036
    
  22
It only takes a few minutes to install Java on another computer.

Anyway, try it as it stands, where Dog has no connection with Collar, and serialize Dog. Then give the Dog a Collar as a field, and try again and see what happens.
sonia jaswal
Ranch Hand

Joined: Jun 01, 2007
Posts: 42
Thanks for the reply....
i will try and let you know... thanks...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serialization
 
Similar Threads
Serialization Problem
A query regarding serialization in Java
Serialization JSCP 6.0 (manually invoking writeObject() )
Doubts in Serialization
Method reference Query