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.
Technically you can't prevent two classes from having the same serial version UID, much like you can't prevent two objects from having the same system hash code.
Simply put, we use the serialVersionUID attribute to remember versions of a Serializable class to verify that a loaded class and the serialized object are compatible. The serialVersionUID attributes of different classes are independent. Therefore, it is not necessary for different classes to have unique values.
File -> Settings -> Editor -> Inspections -> Java -> Serialization issues : Find serialization class without serialVersionUID and check it. Back to the editor, clicks on the class name, ALT + ENTER (Windows), it will prompts the Add serialVersionUID field option. A new serialVersionUID is auto-generated.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With