warning ... no definition of serialVersionUID ?

D

Denis Labon

I compile the following code and it gives me a warning about
"serializable class".
Do some1 know what the warning means?

I compile the code in Suse 9.0 and use java v1.5.0-beta2.

COMPILE COMMAND: javac -Xlint *.java

Thanx!


/*

Test.java:14: warning: [serial] serializable class Test has no
definition of serialVersionUID
public class Test extends JOptionPane
^
1 warning

*/

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Test extends JOptionPane
{
public Test( )
{
super();
}
public static void main( String args[] )
{
}
}
 
T

Tor Iver Wilhelmsen

I compile the following code and it gives me a warning about
"serializable class".
Do some1 know what the warning means?

It means that you risk incompatibilities between different versions of
Java VMs if you use binary serialization of the object. This is
because without an explicit "serialVersionUID" value, the VM will
generate a version number based on the class.

See the docs for java.io.Serializable for more info.

For most cases you can ignore the warning.
 
M

Michael Borgwardt

Tor said:
It means that you risk incompatibilities between different versions of
Java VMs if you use binary serialization of the object.

Not between different JVMs, but between different versions of the class.

For example, if you change the signature of one of the class's methods,
serialized instances of the class will now be considered "incompatible"
with the new class definition, even though only the fields are serialized
and those have not changed.
 
D

Denis Labon

But in the code above, I ONLY derive a class from JOptionPane. I don't
intend to use the serialized instance, besides I don't even understand
serialisable stuff.
So, why the warning. How to make the warning go away?


thanx!
 
T

Tor Iver Wilhelmsen

But in the code above, I ONLY derive a class from JOptionPane. I don't
intend to use the serialized instance, besides I don't even understand
serialisable stuff.

But since you make a subclass of a class implementing Serializable,
your code implicitly implements Serializable. That is the reason.
So, why the warning. How to make the warning go away?

By not running with the -Xlint flag? See if you can pass extra args to
-Xlint to limit what it reports on, like you can for -verbose.
 
T

Tor Iver Wilhelmsen

Tor Iver Wilhelmsen said:
By not running with the -Xlint flag? See if you can pass extra args to
-Xlint to limit what it reports on, like you can for -verbose.

Just checked with javac -X, and it seems

-Xlint:all,-serial

will give you what you want.
 
D

Denis Labon

So, what is the right way to derive a class from JOptionPane without
having any warnings?

thanx!
 
T

Tor Iver Wilhelmsen

So, what is the right way to derive a class from JOptionPane without
having any warnings?

You could add

public static final long serialVersionUID = 24362462L;

to your class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top