Sign in to follow this  
VaderJ

why won't this work??

Recommended Posts

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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Thanks guys. I appreciate the fast replies. :) It does make sense that if something was going to be 'constant' you would have to give it that value up front. Thanks again for your help.

Sorry I didn't specify that well which language I was using. :|

Share this post


Link to post
Share on other sites

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