static final long serialVersionUID = 1L

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

I have 1 simple question about this guided program given by our professor.

I have seen serialVersionUID several times but I dont what's that for.

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.
    }
}

What is the use of

static final long serialVersionUID = 1L;

in the program?


The serialVersionUID is a universal version identifier for a Serializable class. Deserialization uses this number to ensure that a loaded class corresponds exactly to a serialized object. If no match is found, then an InvalidClassException is thrown.


This is why:

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

It's not mandatory, but should be added if possible. doesn't hurt though.

链接地址: http://www.djcxy.com/p/23832.html

上一篇: 什么是串行版本ID?

下一篇: static final long serialVersionUID = 1L