Java中transient关键字

在Java中,一个对象实现了Serilizable接口,就可以实现序列化。但是有些时候某些属性是不需要序列化的,只需要将这些字段加上transient关键字修饰,在序列化时就可以避开这些属性。
如HashMap中的一些字段:

public class HashMap<K,V> extends AbstractMap<K,V>
    implements Map, Cloneable, Serializable {

    private static final long serialVersionUID = 362498820763181265L;
    //......省略10000行......
/**
     * The table, initialized on first use, and resized as
     * necessary. When allocated, length is always a power of two.
     * (We also tolerate length zero in some operations to allow
     * bootstrapping mechanics that are currently not needed.)
     */
    transient Node[] table;

    /**
     * Holds cached entrySet(). Note that AbstractMap fields are used
     * for keySet() and values().
     */
    transient Set> entrySet;

    /**
     * The number of key-value mappings contained in this map.
     */
    transient int size;

    /**
     * The number of times this HashMap has been structurally modified
     * Structural modifications are those that change the number of mappings in
     * the HashMap or otherwise modify its internal structure (e.g.,
     * rehash).  This field is used to make iterators on Collection-views of
     * the HashMap fail-fast.  (See ConcurrentModificationException).
     */
    transient int modCount;

    //......省略10000行......

你可能感兴趣的:(java)