Sign in to follow this  

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

This topic is 1966 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 [i]serialversionUID[/i] = 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
You should look for Serialization.

Looks like that your Hero class is serializable (you declared it that way).
In that case a field named [i]serialVersionUID[/i] 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 [url="http://www.javapractices.com/topic/TopicAction.do?Id=45"]website[/url].
Try understanding this guys implementation.

Share this post


Link to post
Share on other sites
[quote name='kuramayoko10' timestamp='1343605263' post='4964348']
You should look for Serialization. Looks like that your Hero class is serializable (you declared it that way).[/quote]

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:

[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;

}
}
[/CODE] 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
[quote name='DrMadolite' timestamp='1343598201' post='4964317']
Hey, I don't understand this method called "private static final long [i]serialversionUID[/i] = 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".[/quote]

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
[quote]
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

This topic is 1966 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this