0

I am a new java programmer. I am trying to execute this program in eclipse but the error is

The serializable class LoveJava does not declare a static final serialVersionUID field of type long.

private static final long serialVersionUID = 1L; 

Why I have to declare this line?

import acm.graphics.*;
import acm.program.*;

public class LoveJava extends GraphicsProgram {

    private static final long serialVersionUID = 1L;

    public void run(){
        add(new GLabel("I love Java"),100,75);
    }
}
Philipp
  • 67,764
  • 9
  • 118
  • 153

2 Answers2

2

Welcome to java community.

Read specific API before asking here and here is the essential part of API.

The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long:

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

Read blog post "Eclipse: Ignore “Not Declare Static Final SerialVersionUID” Warning" in order to configure Eclipse to ignore this compiler warning.

Or add @SuppressWarnings(“serial”) right before your class declaration to avoid the compiler warning from being raised in the first place. Like this:

import acm.graphics.*;
import acm.program.*;

@SuppressWarnings(“serial”)
public class LoveJava extends GraphicsProgram {

    private static final long serialVersionUID = 1L;

    public void run(){
        add(new GLabel("I love Java"),100,75);
    }
}
tbsalling
  • 4,477
  • 4
  • 30
  • 51
  • 3
    Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Joachim Sauer Jun 10 '13 at 12:51