Results 1 to 4 of 4
  1. #1
    DrMadolite is offline Member
    Join Date
    Jul 2012
    Location
    Norway
    Posts
    29
    Rep Power
    0

    Default How do I control whether a class is declared serializable or not?

    Hi!

    I'm a newcomer to java and there's this problem that I seem to have with Eclipse that none of the tutorial people on youtube seem to have. As I create a new class, there's sometimes (but not always) this thing that happens that gives me an yellow error unless I add the method "private static final long serializedVersionUID = 1L;" to it.

    I've looked around on the internet and I've learned that this should only pop up for a class that has been declared serializable. But I don't get what I could've done to declare it serializable? Is it something I did in the code or some option I overlooked in Eclipse as I created the class?

    In other words, how do I un-serialize a class within java?

    Thanks!




    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.Random;

    import javax.swing.JPanel;
    import javax.swing.Timer;


    public class HeroFrame extends JPanel implements ActionListener {

    /**
    *
    */
    private static final long serialVersionUID = 1L;


    Timer mainTimer;
    Player player;

    int enemyCount = 5;

    static ArrayList<Enemy> enemies = new ArrayList<Enemy>();
    Random rand = new Random();

    public HeroFrame() {
    setFocusable(true);

    player = new Player (100, 100);
    addKeyListener(new KeyAdapt(player));

    mainTimer = new Timer(20, this);
    mainTimer.start();

    for (int i = 0; i < enemyCount; i++) {
    addEnemy(new Enemy(rand.nextInt(800), rand.nextInt(600)));

    }

    }

    public void paint (Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;

    player.draw(g2d);

    for (int i = 0; i < enemies.size(); i++) {
    Enemy tempEnemy = enemies.get(i);
    tempEnemy.draw(g2d);

    }

    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
    player.update();
    repaint();
    }

    public void addEnemy(Enemy e) {
    enemies.add(e);

    }

    public static void removeEnemy(Enemy e) {
    enemies.remove(e);

    }

    public static ArrayList<Enemy> getEnemyList() {
    return enemies;

    }

    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    9

    Default Re: How do I control whether a class is declared serializable or not?

    Serializable is an interface
    Serializable Objects (The Java™ Tutorials > Java Naming and Directory Interface > Java Objects in the Directory)
    Component implements Serializable, JPanel extends Component, and your class extends JPanel. Thus, by inheritance, your class implements Serializable.
    http://docs.oracle.com/javase/6/docs...ng/JPanel.html

    The yellow icon is not an error, it is a warning. If you wish to suppress the warning, add the following...
    Java Code:
    @SuppressWarnings("serial")
    ...above the class definition. That being said, why not just add the serialVersionUID variable?

  3. #3
    DrMadolite is offline Member
    Join Date
    Jul 2012
    Location
    Norway
    Posts
    29
    Rep Power
    0

    Default Re: How do I control whether a class is declared serializable or not?

    Quote Originally Posted by doWhile View Post
    That being said, why not just add the serialVersionUID variable?
    Thanks for the answer. The reason why I feel reluctant about adding the serialVersionUID variable is just because I was unsure what it did and I read something here:

    Java Practices -> Implementing Serializable
    "Do not implement Serializable lightly, since it restricts future flexibility, and publicly exposes class implementation details which are usually private."
    I think I'll just ignore the warning for now and maybe add that annotation. Thanks again.
    Last edited by DrMadolite; 07-30-2012 at 04:38 AM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,300
    Blog Entries
    7
    Rep Power
    24

    Default Re: How do I control whether a class is declared serializable or not?

    Eclipse can add the serialVersionUID for you: right click on the warning icon and select 'Quick Fix'; select 'Add generated serial verion UID' and voila.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Class is public, should be declared in a file
    By goldhouse in forum New To Java
    Replies: 7
    Last Post: 05-10-2012, 09:17 AM
  2. Replies: 2
    Last Post: 10-21-2011, 07:38 AM
  3. ObjectInputStream and the Serializable class
    By Junky in forum Advanced Java
    Replies: 5
    Last Post: 08-15-2011, 01:46 PM
  4. Replies: 1
    Last Post: 07-10-2011, 04:33 PM
  5. Replies: 2
    Last Post: 12-05-2009, 12:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •