• 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

Serialization

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...???
 
Marshal
Posts: 79464
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is,
.
.
.
.
.
.
.
NO.

Try it and see what actually happens.
 
Campbell Ritchie
Marshal
Posts: 79464
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79464
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply....
i will try and let you know... thanks...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic