Which variables will and will not be restored with the appropriate values when an object is deserialized : Serialization « File « SCJP

Home
SCJP
1.Java Source And Data Type
2.Operators
3.Modifiers
4.Type Casting
5.Statements
6.Object Oriented
7.Thread
8.Utility Classes
9.File
SCJP » File » Serialization 
9.5.7.Which variables will and will not be restored with the appropriate values when an object is deserialized
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class MainClass {
  public static void main(String[] args) {
    Dog d = new Dog(35"Fido");
    System.out.println("before: " + d.name + " " + d.weight);
    try {
      FileOutputStream fs = new FileOutputStream("testSer.ser");
      ObjectOutputStream os = new ObjectOutputStream(fs);
      os.writeObject(d);
      os.close();
    catch (Exception e) {
      e.printStackTrace();
    }
    try {
      FileInputStream fis = new FileInputStream("testSer.ser");
      ObjectInputStream ois = new ObjectInputStream(fis);
      d = (Dogois.readObject();
      ois.close();
    catch (Exception e) {
      e.printStackTrace();
    }

    System.out.println("after:  " + d.name + " " + d.weight);
  }
}

class Dog extends Animal implements Serializable {
  String name;

  Dog(int w, String n) {
    weight = w; // inherited
    name = n; // not inherited
  }
}

class Animal // not serializable !
  int weight = 42;
}
before: Fido 35
after:  Fido 42
9.5.Serialization
9.5.1.Object Serialization
9.5.2.Object Streams and Serialization
9.5.3.Wirte your own serialization code
9.5.4.Working with ObjectOutputStream and ObjectInputStream
9.5.5.Serialize a hierarchy
9.5.6.Using writeObject and readObject
9.5.7.Which variables will and will not be restored with the appropriate values when an object is deserialized
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.