private static final long method - someone please explain the logic?

Started by
4 comments, last by rip-off 11 years, 8 months ago
Hey, I don't understand this method called "private static final long serialversionUID = 1L;" that causes a small error if I don't add it ("the serializable class HeroFrame does not declare a static final serialversionUID field of type long"). To resolve it I had to "add default serial version ID".

Another funky thing is that this class is a copy off a tutorial guy (baseball3455) on youtube and our classes are identical, except for the class name (his was called GameFrame). The point is that he never had to add the ID and it worked fine for him, it never showed an error.

Could someone please explain what this method does exactly and why it's needed, as well as telling me why it's needed only by my gameframe class (my converging class after my main) and not any other classes including the main? I mean, I got some ideas but I just don't get it. I can understand the versioning requirement like you would need to define a doc-type to a HTML document, but wouldn't that apply to the main? It'd be nice to hear some details.

Thanks in advance.

- Awl you're base are belong me! -

- I don't know, I'm just a noob -

Advertisement
You should look for Serialization.

Looks like that your Hero class is serializable (you declared it that way).
In that case a field named serialVersionUID is very important (and required in Java) because it identifies unically your class and will be important to validate the objects you will be retrieving in the future.

Searching on Google I found this example website.
Try understanding this guys implementation.
Programming is an art. Game programming is a masterpiece!

You should look for Serialization. Looks like that your Hero class is serializable (you declared it that way).


How did I declare it serializable? I'm sorry I'm a noob. I didn't make any related methods in the class, afaik. Could it be something I did when I created the class? (I'm using eclipse) I'm finding information about what serialization means and what to do when you use it, but I'm finding no information about how to stop the class from being automatically declared serializable to begin with.

Here's the class code:


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;

}
}

- Awl you're base are belong me! -

- I don't know, I'm just a noob -

Nevermind, I got an answer to that second part from a different forum. Serializable is a part of Components and JPanel extends Components so since I used a JPanel, I'm extending Components which implements serializable.

Thanks for your answer too, though. Appreciate it.

- Awl you're base are belong me! -

- I don't know, I'm just a noob -


Hey, I don't understand this method called "private static final long serialversionUID = 1L;" that causes a small error if I don't add it ("the serializable class HeroFrame does not declare a static final serialversionUID field of type long"). To resolve it I had to "add default serial version ID".


By the way, that is not a method call, that is the declaration of a class constant. And missing it should not generate an error in general, just a warning.

Another funky thing is that this class is a copy off a tutorial guy (baseball3455) on youtube and our classes are identical, except for the class name (his was called GameFrame). The point is that he never had to add the ID and it worked fine for him, it never showed an error.
[/quote]
You can configure your IDE to globally suppress these warnings. If you start writing classes that you intend to serialise, then this probably isn't a good idea. You can also suppress these warnings on a per-class basis using the @SuppressWarnings("serial") annotation.

This topic is closed to new replies.

Advertisement