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. You can give your variables any names you like but should be consistent in how you name variables. This section outlines the generally recommended naming method for variables.

Java is case-sensitive when it comes to variable names, so you must always capitalize variable names the same way. For example, if the gameOver variable is referred to as GameOver somewhere in the program, an error prevents the program from being compiled.

A variable’s name should 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 want 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 works:

int all-TimeHigh Score;
int all Time High Score;

If you try these variable names in a program, NetBeans responds by flagging the error with the red alert icon alongside the line in the source editor.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.183.149