@SuppressWarnings(“串行”)
我有一个问题,因为我有点困惑(或者也许我没有注意到一些明显的事情)。假设我有一些源代码,其中包含很多类,其中包含大量定义如下的静态字段:
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));
}
};
}
众所周知,静态字段不会被序列化。
然而,Java(和 Eclipse)抱怨“可序列化类没有声明 long 类型的静态最终 serialVersionUID 字段”。为什么他们没有注意到 static 不会被序列化?
下一个问题:使用 @SuppressWarnings("serial") 消除所有此类警告是否是解决此问题的正确方法?
编辑:
我的类都没有实现 Serialized 接口(或者它们的超类都没有)。 Eclipse 指向 HashMap
并发出警告。为什么它没有检测到它是静态字段?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仅仅因为该字段可能不会被序列化,并不意味着它引用的内容本身永远不会被序列化!其他人/事物可以获取对该映射的引用并尝试直接序列化它,或者将其用作可序列化类中的实例成员等。我看到它是私有的,但确保永远不会在当前类之外访问它或设置为实例成员超出了编译器的范围(无论如何反射都是不可能的)。
一种可能的解决方案是简单地避免使用带有初始值设定项样式的匿名子类,并执行以下操作:
在大多数情况下,结果接近相同,并且您的代码中不会充斥着匿名类。
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:
The result is close to identical in most cases and your code isn't peppered with anonymous classes.
错误消息和您的类中有一个最终静态成员变量这一事实(至少,这就是我解释您的描述的方式)彼此没有任何关系。
您的类实现了接口
Serialized
,或者您的类的超类之一实现了该接口。所以编译器注意到你的类是可序列化的。可序列化的类应该有一个名为serialVersionUID
的静态最终字段,用于在序列化类的实例时进行版本控制。使用注释
@SuppressWarnings("serial")
会使编译器对缺少的serialVersionUID
关闭。所以,是的,您可以使用它来消除警告消息,但更好的解决方案是,如果您的类不打算被序列化,则使您的类不实现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 calledserialVersionUID
which is used for versioning when you serialize instances of your class.Using the annotation
@SuppressWarnings("serial")
makes the compiler shut up about a missingserialVersionUID
. So yes, you can use that to get rid of the warning message, but a better solution is to make your class not implementSerializable
(directly or indirectly) if it's not meant to be serialized.