Previous Section  < Day Day Up >  Next Section

Naming Your Variables

Variable names in Java can begin with a letter, underscore character (_), or a dollar sign ($). The rest of the name can be any letters or numbers, but you cannot use blank spaces. You can give your variables any names you like under those rules, but you should be consistent in how you name variables. This section outlines the generally recommended naming method for variables.

Watch Out!

Java is case-sensitive when it comes to variable names, so you must always capitalize variable names in the same way throughout a program. For example, if the gameOver variable is used as GameOver somewhere in the program, the use of GameOver will cause an error when you compile the program.

For starters, the name you give a variable will describe its purpose in some way. The first letter should be lowercase, and if the variable name has more than one word, make the first letter of each subsequent word a capital letter. For instance, if you wanted to create an integer variable to store the all-time high score in a game program, you can use the following statement:

int allTimeHighScore;

You can't use punctuation marks or spaces in a variable name, so neither of the following will work:

int all-TimeHigh Score;

int all Time High Score;

If you tried to use these names in a program, the Java compiler will respond with an error.

    Previous Section  < Day Day Up >  Next Section