Jump to content
  • Advertisement
Sign in to follow this  
Ronnie Mado Solbakken

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

This topic is 2182 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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. Edited by DrMadolite

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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;

}
}
Edited by DrMadolite

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!