zoukankan      html  css  js  c++  java
  • 关于java序列化中的一个细节

    java序列化机制的可以参考很多资料了,最近在看的时候发现了一些问题。

    1.

    默认的序列化机制,很多书里讲到序列化类只序列化类名,实例变量,不会实例化类变量(static)和瞬态变量(transient).

    我使用1.6,1.7,1.8测试了一下,static都是可以被序列化的。

    测试代码:

     1 public class Logg implements Serializable{
     2   
     3     private static String name;
     4     private transient String pwd;
     5     
     6     public void setName(String pwd){
     7            Logg.name=name;
     8     }
     9 
    10     public void setPwd(String pwd){
    11            this.pwd=pwd;
    12     }
    13 
    14     
    15     public String toString() {
    16         return "name="+name+",pwd="+pwd;
    17     }
    18 }    
     1 ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("log.dat"));
     2 ObjectInputStream in = new ObjectInputStream(new FileInputStream("log.dat"));
     3 
     4 Logg logg = new Logg();
     5 logg.setName("test");
     6 logg.setPwd("111111");
     7 
     8 out.writeObject(logg);
     9 out.close();
    10 Logg log = (Logg)in.readObject();
    11 in.close();
    12 System.out.println(log.toString());
  • 相关阅读:
    17. Letter Combinations of a Phone Number
    16. 3Sum Closest
    15. 3Sum
    14. Longest Common Prefix
    13. Roman to Integer
    12. Integer to Roman
    11. Container With Most Water
    10. Regular Expression Matching
    9. Palindrome Number
    8. String to Integer (atoi)
  • 原文地址:https://www.cnblogs.com/selfchange/p/6031443.html
Copyright © 2011-2022 走看看