Advertisement
bestsss

Serialization c-tor invocation.

Oct 26th, 2014
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. package t1;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.ObjectInputStream;
  6. import java.io.ObjectOutputStream;
  7.  
  8. public class SerCtor {
  9.   static class A{
  10.     A(){
  11.       System.out.println("A: "+System.currentTimeMillis());
  12.     }
  13.   }
  14.   static class B extends A implements java.io.Serializable{
  15.     final int n;
  16.     B(int n){
  17.       this.n = n;
  18.       System.out.println("B: "+System.currentTimeMillis());
  19.  
  20.     }
  21.   }
  22.  
  23.   public static void main(String[] args) throws Throwable{
  24.     B b = new B(11);
  25.     System.out.println("B: "+b.n);
  26.    
  27.     ByteArrayOutputStream buf = new ByteArrayOutputStream();
  28.     ObjectOutputStream out =new ObjectOutputStream(buf);
  29.     out.writeObject(b);
  30.     out.close();
  31.     System.out.println("deserialize");
  32.    
  33.     ObjectInputStream in =new ObjectInputStream(new ByteArrayInputStream( buf.toByteArray()));
  34.     b = (B) in.readObject();
  35.     in.close();
  36.     System.out.println("B: "+b.n);
  37.   }
  38. }
  39. /*
  40. Output, something like:
  41.  
  42. A: 1414349969582
  43. B: 1414349969582
  44. B: 11
  45. deserialize
  46. A: 1414349969595
  47. B: 11
  48. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement