静态Final Long serialVersionUID = 1L

时间:2013-01-11 08:45:37

标签: java jsp

  

可能重复:
  What is a serialVersionUID and why should I use it?
  what is meant by that in a servlet (private static final long serialVersionUID = 1L)?

我有一个关于这个由我们的教授给出的指导计划的简单问题。 我已经多次看过serialVersionUID,但我不知道那是为了什么。

package module;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author JA
 */
public class Servlet_1 extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet{

    static final long serialVersionUID = 1L;

    public void doGet (HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        response.setContentType("txt/html");

        ServletOutputStream out = response.getOutputStream();

        out.println("<html>");
        out.println("<head><title>Hello Panget</title></head>");
        out.println("<body>");
        out.println("<h1>Hello Panget</h1>");
        out.println("</body></html>");
    }

    protected void doPost (HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            // Write your program here, Panget.
    }
}

程序中static final long serialVersionUID = 1L;的用途是什么?

2 个答案:

答案 0 :(得分:32)

serialVersionUIDSerializable类的通用版本标识符。反序列化使用此数字来确保加载的类与序列化对象完全对应。如果未找到匹配项,则抛出InvalidClassException

答案 1 :(得分:8)

这就是原因:

http://frequal.com/java/PracticalSerialVersionIdGuidelines.html

这不是强制性的,但应尽可能添加。虽然没有伤害。