Results 1 to 4 of 4
- 07-30-2012, 03:27 AM #1
Member
- Join Date
- Jul 2012
- Location
- Norway
- Posts
- 29
- Rep Power
- 0
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;
}
}
- 07-30-2012, 04:00 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
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...
...above the class definition. That being said, why not just add the serialVersionUID variable?Java Code:@SuppressWarnings("serial")
- 07-30-2012, 04:24 AM #3
Member
- Join Date
- Jul 2012
- Location
- Norway
- Posts
- 29
- Rep Power
- 0
Re: How do I control whether a class is declared serializable or not?
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
I think I'll just ignore the warning for now and maybe add that annotation. Thanks again."Do not implement Serializable lightly, since it restricts future flexibility, and publicly exposes class implementation details which are usually private."Last edited by DrMadolite; 07-30-2012 at 04:38 AM.
- 07-30-2012, 08:34 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Class is public, should be declared in a file
By goldhouse in forum New To JavaReplies: 7Last Post: 05-10-2012, 09:17 AM -
Class<T> implements Serializable? What are the Implications?
By kennyman94 in forum Advanced JavaReplies: 2Last Post: 10-21-2011, 07:38 AM -
ObjectInputStream and the Serializable class
By Junky in forum Advanced JavaReplies: 5Last Post: 08-15-2011, 01:46 PM -
I need to keep a whole file as a serializable class field
By urbanleg in forum New To JavaReplies: 1Last Post: 07-10-2011, 04:33 PM -
local variable tf1 is accessed from within inner class; needs to be declared final
By shoeb83 in forum New To JavaReplies: 2Last Post: 12-05-2009, 11:24 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks