Externalization Continues:
As i mentioned earlier, in Serialization, transient keyword plays a very important role. Now, let's see what transient keyword can do in Externalization.
To understand transient keywords significance, it is better to see the code below first
1) If the class implements Serializable Interface then total Object will be saved to the file and the Output is
ComputerScienceMatters 10 20
2) If the class Implements Externalizable Interface then part of the Object will be saved to the file and Output is
Public No-Arg Constructor
ComputerScienceMatters 10 20
Note:
1) transient keyword play role only in Serialization and it won't play role in Externalization. Of course transient keyword is not required in Externalization.
2) If all variables declared as transient and class implements Serializable then the output is
null 0 0
3) If all variables declared as transient and class implements Externalizable then the output is
Public No-Arg Constructor
ComputerScienceMatters 0 0
As i mentioned earlier, in Serialization, transient keyword plays a very important role. Now, let's see what transient keyword can do in Externalization.
To understand transient keywords significance, it is better to see the code below first
import java.io.*; class ExternalizableDemo implements Externalizable{ String s; int i; int j; public ExternalizableDemo(){ System.out.println("Public No-Arg Constructor"); } public ExternalizableDemo(String s; int i; int j){ this.s =s; this.i=i; this.j=j; } public void writeExternal(ObjectOutput out) throws IOException{ out.writeObject(s); out.writeObject(i); out.writeObject(j); } public void readObject(ObjectInput in) throws IOException, ClassNotFoundException{ s= (String)in.readObject(); i= in.readInt(); j= in.readInt(); } public static void main(String []args){ ExternalizableDemo t1 = new ExternalizableDemo("ComputerScienceMatters", 10,20); FileOutputStream fos = new FileOutputStream("xyz.ser"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(t1); FileInputStream fis = new FileInputStream("xyz.ser"); ObjectInputStream ois = new ObjectInputStream(fis); ExternalizableDemo t2 = (ExternalizableDemo)ois.readObject(); System.out.println(t2.s+ " "+t2.i+" "+t2.j); } }
1) If the class implements Serializable Interface then total Object will be saved to the file and the Output is
ComputerScienceMatters 10 20
2) If the class Implements Externalizable Interface then part of the Object will be saved to the file and Output is
Public No-Arg Constructor
ComputerScienceMatters 10 20
Note:
1) transient keyword play role only in Serialization and it won't play role in Externalization. Of course transient keyword is not required in Externalization.
2) If all variables declared as transient and class implements Serializable then the output is
null 0 0
3) If all variables declared as transient and class implements Externalizable then the output is
Public No-Arg Constructor
ComputerScienceMatters 0 0
0 comments:
Post a Comment