Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@SuppressWarnings("serial")

I have a question because I'm getting a little confused (or maybe I'm not noticing something obvious). Let's say that I've got some source code that contains a lot of classes which contain a great number of static fields defined like this one:

public final class ConverterTYPE  {
    private final static HashMap<String, Byte> STRING_MAP = new HashMap<String, Byte>() {
        {
            put("A", new Byte((byte)12));
            put("B", new Byte((byte)13));
        }
    };

}

As we all know, static fields are not going to be serialized.

However, Java (and Eclipse) complains that "The serializable class does not declare a static final serialVersionUID field of type long". Why can't they notice that static is not going to be serialized?

And the next question: would it be a right solution to this issue to use @SuppressWarnings("serial") to get rid of all such warnings?

EDIT:

None of my classes implements Serializable interface (or none of their superclasses). And Eclipse is pointing at HashMap<String, Byte> with its warnings. Why doesn't it detect that it's static field?

like image 547
Lukasz Avatar asked Jan 20 '11 15:01

Lukasz


People also ask

What is @SuppressWarnings serial?

Using the annotation @SuppressWarnings("serial") makes the compiler shut up about a missing serialVersionUID . So yes, you can use that to get rid of the warning message, but a better solution is to make your class not implement Serializable (directly or indirectly) if it's not meant to be serialized.

What is that @SuppressWarnings serial in Java?

The line @SuppressWarnings(\"serial\") tells Java not to remind you that you've omitted something called a serialVersionUID field.

What does @SuppressWarnings unused mean?

The @SuppressWarnings annotation disables certain compiler warnings. In this case, the warning about deprecated code ( "deprecation" ) and unused local variables or unused private methods ( "unused" ).

What is @SuppressWarnings resource?

Adding the @SuppressWarnings({ "resource" }) should remove the warning for a potential resource leak. Adding a list for other ones, it's for Eclipse but they should be fairly similar for reference.


2 Answers

Just because that field may not be serialized doesn't mean the thing it references will never itself be serialized! Someone/thing else could get a reference to that map and try to serialize it directly, or use it as an instance member in a serializable class, etc.I see it is private, but making sure it will never be accessed outside the current class or set to an instance member is beyond the scope of the compiler (and impossible with reflection around anyway).

One possible solution is to simply avoid that anonymous subclass w/ initializer style all together and do this:

private final static HashMap<String, Byte> STRING_MAP = new HashMap<String, Byte>();

static {  
  STRING_MAP.put("A", new Byte((byte)12));
  STRING_MAP.put("B", new Byte((byte)13));
}

The result is close to identical in most cases and your code isn't peppered with anonymous classes.

like image 75
Affe Avatar answered Oct 27 '22 21:10

Affe


However, Java (and Eclipse) complains that "The serializable class does not declare a static final serialVersionUID field of type long". Why can't they notice that static is not going to be serialized?

The error message and the fact that you have a final static member variable in your class (at least, that's how I interpret your description) don't have anything to do with each other.

Your class implements the interface Serializable, or one of the superclasses of your class does that. So the compiler notices that your class is serializable. Serializable classes should have a static final field called serialVersionUID which is used for versioning when you serialize instances of your class.

Using the annotation @SuppressWarnings("serial") makes the compiler shut up about a missing serialVersionUID. So yes, you can use that to get rid of the warning message, but a better solution is to make your class not implement Serializable (directly or indirectly) if it's not meant to be serialized.

like image 43
Jesper Avatar answered Oct 27 '22 23:10

Jesper