There are 2 class ,A and B Class  A implements Sierializible{
private static final long serialVersionUID = -783680237161881281L;
  private  B varB1;
  protected B varB2;
  public B varB3;
}Then I got the warning on both three field in FindBugs Below:
[Se] Non-transient non-serializable instance field in serializable class [SE_BAD_FIELD]
This Serializable class defines a non-primitive instance field which is neither transient, Serializable, or java.lang.Object, and does not appear to implement the Externalizable interface or the readObject() and writeObject() methods.  Objects of this class will not be deserialized correctly if a non-Serializable object is stored in this field.How could I change my code 
Must I let Class B also implements Serializable if I would like to use the varBx also after a 
deserialized ?Thanks !

解决方案 »

  1.   

    class B :/*
     * @(#)CyclicBarrier.java 1.12 06/03/30
     *
     * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
     * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
     */package java.util.concurrent;
    import java.util.concurrent.locks.*;
      

  2.   

    Objects of this class will not be deserialized correctly if a non-Serializable object is stored in this field. 
    这句话就可以作为你判断B要不要实现Sierializible的依据了
      

  3.   

    How come if the class B is like javax.naming.directory.DirContext? The interface DirContext is unable to be changed.Sometimes you need to add "transient" keyword --
    If you create a class A that has fields that you don’t want saved (or
    can’t be saved because the object type of the fields themselves don’t
    implement Serializable)
    Class  A implements Sierializible{ 
    private static final long serialVersionUID = -783680237161881281L; 
      transient B varB1;
      

  4.   

    在这种情况下,就要使用writeObject 以及 readObject来定制序列化了。
    比如一个文件对象引用,比如一个Thread对象。