why won't this work??
I am going through some java game tutorials and I came across something I don't understand. It has to do with constants.
Example 1:
// game state constants
private static final int GAME_STATE_INTRO = 0 ;
private static final int GAME_STATE_SETUP = 1;
private static final int GAME_STATE_PLAYING = 2;
private static final int GAME_STATE_GAMEOVER = 3;
putting this in my game compiles fine, but when I put this..
Example 2:
// game state constants
private static final int GAME_STATE_INTRO;
private static final int GAME_STATE_SETUP;
private static final int GAME_STATE_PLAYING;
private static final int GAME_STATE_GAMEOVER;
// initialize game state constants
GAME_STATE_INTRO = 0;
GAME_STATE_SETUP = 1;
GAME_STATE_PLAYING = 2;
GAME_STATE_GAMEOVER = 3;
I get an error saying that an identifier is expected. This is all happening before main. I didn't think there was any problem with delcaring the variables and initializing them seperately.
Could someone please help me understand the rules a little better of why this doesn't work? Thanks
I assue from the private static final thingy you are in Java, so I have two things.
1. private static final int
The keyword "final" there indicates that the variable cannot be changed after initialization. So you usually give it a value in the same statement as the declaration, as in your first (working) example.
2. outside of methods variable declarations are permitted, but assignments are not. So:
private static final int GAME_STATE_INTRO = 0;
is perfectly valid outside of a method. While:
GAME_STATE_INTRO = 0;
is not, unless you did that in main, but then a conflict might arise over the final keyword, not sure.
1. private static final int
The keyword "final" there indicates that the variable cannot be changed after initialization. So you usually give it a value in the same statement as the declaration, as in your first (working) example.
2. outside of methods variable declarations are permitted, but assignments are not. So:
private static final int GAME_STATE_INTRO = 0;
is perfectly valid outside of a method. While:
GAME_STATE_INTRO = 0;
is not, unless you did that in main, but then a conflict might arise over the final keyword, not sure.
From what I remember, constants, in most languages, must be assigned values right when they are defined. You can't late-bind a value to a constant, because technically, that's changing the value, which defies the whole purpose of having the constant in the first place. So it should be like:
And that should work. You can also just remove the constant keyword (which I'm assuming is "final" - I don't know much Java).
private static final int GAME_STATE_INTRO = 0;private static final int GAME_STATE_SETUP = 1;private static final int GAME_STATE_PLAYING = 2;private static final int GAME_STATE_GAMEOVER = 3;
And that should work. You can also just remove the constant keyword (which I'm assuming is "final" - I don't know much Java).
Quote:Original post by Venerable Vampire
[...] but then a conflict might arise over the final keyword, not sure.
The conflict does definately arise over the final keyword. Final in a field or static field makes the variable a constant, you can't change its value after initialization (which also implies you have to specify a value for initialization).
And VaderJ: Always make sure to include all your relevant information in your post. In this case, this would be which language you are referring to.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement