The serializable class does not declare a static final serialversionuid field of type long warning appears when compiling the following Java program
1 Packagelearn;2 3 Importjavax.swing.*;4 Importjava.awt.*;5 Importjava.awt.event.*;6 7 Public classsimplegui3cImplementsactionlistener{8 9 JFrame frame;Ten One Public Static voidMain (string[] args) { Asimplegui3c GUI =Newsimplegui3c (); - gui.go (); - } the - Public voidGo () { -frame =NewJFrame (); - frame.setdefaultcloseoperation (jframe.exit_on_close); + -JButton button =NewJButton ("Change Color"); +Button.addactionlistener ( This); A atMydrawpanel Drawpanel =NewMydrawpanel (); - - Frame.getcontentpane (). Add (Borderlayout.south, button); - Frame.getcontentpane (). Add (Borderlayout.center, drawpanel); -Frame.setsize (600, 300); -Frame.setvisible (true); in } - to Public voidactionperformed (ActionEvent event) { + frame.repaint (); - } the}
View Code
Packagelearn;ImportJava.awt.Color;ImportJava.awt.GradientPaint;ImportJava.awt.Graphics;ImportJava.awt.Graphics2D;ImportJavax.swing.JPanel; Public classMydrawpanelextendsjpanel{//private static final long serialversionuid = 6201378234876555585L; Public voidpaintcomponent (Graphics g) {graphics2d g2d=(graphics2d) G; intRed = (int) (Math.random () * 255); intGreen = (int) (Math.random () * 255); intBlue = (int) (Math.random () * 255); Color StartColor=NewColor (red, green, blue); Red= (int) (Math.random () * 255); Green= (int) (Math.random () * 255); Blue= (int) (Math.random () * 255); Color EndColor=NewColor (red, green, blue); Gradientpaint Gradient=NewGradientpaint (StartColor, 150, 150, EndColor); G2d.setpaint (gradient); G2d.filloval (70, 70, 100, 100); }}
View Code
To understand this error message, we need to understand a few questions first:
Q: What is a serialversionuid?
a: invalidclassexception serialversionuid
long
:
Private Static Final long serialversionuid = 6201378234876555585L;
SerialVersionUID
is a unique identifier for each class, JVM
uses it to compare the versions of the class ensuring, the same class was used during serialization is loaded Dur ing deserialization. You must declare serialversionuid because it give us more control .
Q:What happen if I don ' t want to declare a serialversionuid
A: If a serializable class does not explicitly declare A Serialversionuid and then the serialization Runti Me would calculate a default Serialversionuid value for this class based on various aspects of the class, as described in T He Java (TM) Object serialization specification.
However, it is strongly recommended -all serializable classes explicitly declare serialversionuid values, sin Ce the default serialversionuid computation is highly sensitive to class details that may vary depending on compiler imple Mentations, and can thus result in unexpected InvalidClassExceptions
during deserialization. Therefore, to guarantee a consistent Serialversionuid value across different Java compiler implementations, a serializable Class must declare an explicit SERIALVERSIONUID value. It is also strongly advised this explicit SERIALVERSIONUID declarations use the private modifier where possible, since suc H declarations apply only to the immediately declaring Class--serialversionuid fields is not useful as inherited members.
Q:why should I use Serialversionuid?
A: Regardless of what serialized form you choose, declare a explicit serial version UID in every serializable class Write. This eliminates the serial version UID as a potential source of incompatibility (Item 74). There is also a small performance benefit. IF No serial version UID is provided, an expensive computation are required to generate one at runtime.
Q:if I wanna ignore such warning, what is should I do?
A: Window > Preferences > Java > Compiler > Errors/warnings > Potential programming problems (only eclipse)
The serializable class does not declare a static final serialversionuid field of type long