Return to Unit Menu | | | Terms of Use  |

Naming Rules for Variables 

The names of variables in the Java language are referred to as identifiers.  

Choosing a variable name:
The best naming convention is to choose a variable name that will tell the reader of the program what the variable represents.  Variable names that describe the data stored at that particular memory location are called mnemonic variables.  For example, if you wish to store a grade on a test, a variable name such as "grade" would be more easily remembered than a variable name such as  "var2".

Rules for naming variables:
pin All variable names must begin with a letter of the alphabet, an underscore, or ( _ ), or a dollar sign ($).  The convention is to always use a letter of the alphabet.  The dollar sign and the underscore are usually discouraged as the first character.

pin After the first initial letter, variable names may also contain additional letters and the digits 0 to 9.  No spaces or special characters are allowed. 

pin The name can be of any length, but don't get carried away.  Remember that you will have to type this name.

pin Uppercase characters are distinct from lowercase characters.  Using ALL uppercase letters are primarily used to identify constant variables.  Remember that variable names are case-sensitive.

pin You cannot use a Java keyword (reserved word) for a variable name.


Tip:  Some programmers use underscores in variable names to separate parts of the name, such as shipping_weight.  Others prefer a "capital style" notation, such as shippingWeight to separate parts of the name.  NEVER use uppercase for every letter in a variable name, because uppercase names are reserved for final (constant) variables.


Samples of acceptable variable names:   YES Samples of unacceptable variable names:   NO
Grade  Grade(Test)
GradeOnTest GradeTest#1
Grade_On_Test 3rd_Test_Grade
GradeTest Grade Test   (has a space)


Return to Unit Menu | | | Terms of Use  |

Notice: These materials are free for your on-line use at this site, but are not free for the taking.
Please do not copy these materials or re-post them to the Internet, as it is copyright infringement.
If you wish hard-copies of these materials, refer to our subscription area,
Help us keep these resources free. Thank you.