Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static Final Long serialVersionUID = 1L [duplicate]

Tags:

java

jsp

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?

like image 553
j g Avatar asked Jan 11 '13 08:01

j g


People also ask

What does serialVersionUID 1l mean?

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.

What happens if two classes have same serialVersionUID?

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.

What does serialVersionUID mean?

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.

How do I get serialVersionUID?

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.


1 Answers

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.

like image 118
Achintya Jha Avatar answered Sep 21 '22 02:09

Achintya Jha