Variables in Java
An identifier is a sequence of one or more characters used to name classes, variables, and subroutines.
Why Programs Need Names
A Java program works with many things at once: classes, variables, and subroutines. To refer to any of them, the program needs a name. In Java, these names are called identifiers. Once you have a name for something, you can read it, change it, and pass it around, so the name is the handle your code uses to reach the thing it stands for.
A common assumption is that any readable text can serve as a name. It cannot. Java applies rules to the characters and structure of every identifier, and this article walks through those rules, then shows how a variable is declared and why that declaration matters. Follow along by tracing what happens to each name as the program is read, one step at a time.
What Makes a Legal Identifier
An identifier is a sequence of one or more characters used to name classes, variables, and subroutines.
Java adds two rules on top of that definition. An identifier must begin with a letter or an underscore. After the first character, it must consist entirely of letters, digits, and underscores. Identifiers cannot contain spaces. Those rules are the whole character-level test, and every candidate name must pass all of them.
Read the diagram column by column. total_1 passes every rule: it starts with a letter, and the underscore and digit both fall within the allowed characters after the first position. Total also passes, because the rules do not forbid uppercase letters. my var fails because a space is not allowed anywhere in an identifier. 2fast fails for a different reason: its first character is a digit, and an identifier must begin with a letter or underscore. Notice that the illegal names break different rules, so when a name is rejected you should check the start first and then scan for spaces or other forbidden characters.
Reading the Declaration Pattern
A declaration is the statement that introduces a variable to the program. The simple Java declaration pattern is a type name, followed by a variable name, followed by a semicolon. The type name tells the program what kind of variable this is. The variable name is an identifier, so it must follow the rules from the previous section. The semicolon ends the statement.
- int is the type name, telling the program this is an integer variable
- count is the identifier chosen for the variable
- the semicolon ends the declaration statement
Java also allows one declaration to introduce several variables, with the names separated by commas. For example, a single statement can declare two names of the same type. Even so, readable style usually favors one variable per statement unless the variables are closely related, because a separate line makes each variable's purpose easier to find and comment on.
Read the diagram from left to right, since the tokens must appear in this order. The type comes first, then the identifier. The equals sign and the value after it are an addition to the basic pattern: they give the variable a starting value at the moment it is declared. The source pattern itself shows only the type and the name, so the simplest declaration is int count; and the extra part is optional.
Declaration Before Use
Declaration is the required introduction of a variable before its use. It introduces the variable's name and tells the program what kind of variable it is.
A Java program cannot use a variable until that variable has been declared. If a line refers to a name and no matching declaration appeared earlier in the code, the program has no established meaning for that name, and the mistake must be corrected before the program can use it. Declarations therefore come first in the flow of the code, and uses come after them.
What do you think happens?
After the statements score, Score, and SCORE are all declared, how many separate variables exist?
Reveal answer
3
Java treats uppercase and lowercase letters as different, so score, Score, and SCORE are three distinct identifiers and therefore three separate variables.
How a Declaration Reaches Memory
A declaration has two connected effects. First, it introduces a name for the program to recognize. Second, when the declaration is executed, the computer sets aside memory for the variable and associates its name with that memory. The name and the storage are two different things that are linked by the declaration. The name is what your code writes; the storage is the space the computer reserves for the value.
Read the diagram as a link between two parts. The left box is the name count, which lives in your source code. The right box is the storage that the computer sets aside when the declaration runs. The arrow is the association the declaration creates. Because the name and the storage are separate, the link is what allows later lines that use count to reach the storage that was reserved for it.
Common Naming Mistakes
Common mistake Using a space inside a name
What it looks likemy var
Why it failsIdentifiers cannot contain spaces.
Do this insteadUse an underscore or a single word, such as my_var or myVar.
Common mistake Starting a name with a digit
What it looks like2fast
Why it failsAn identifier must begin with a letter or underscore.
Do this insteadStart with a letter or underscore, such as fast2 or _fast.
Common mistake Using a reserved word as a name
What it looks likeint
Why it failsReserved words such as class, int, and return already have a fixed meaning in Java and are outside the set of legal identifiers.
Do this insteadPick a descriptive name that is not a reserved word, such as total or itemCount.
Common mistake Assuming capitalization does not matter
What it looks likescore and Score used as if they were the same
Why it failsUppercase and lowercase letters are different, so the two names refer to different identifiers.
Do this insteadChoose one spelling and use it consistently everywhere the variable appears.
Common mistake Using a variable before declaring it
What it looks likea line that uses total with no earlier declaration of total
Why it failsA Java program cannot use a variable until it has been declared.
Do this insteadPlace the declaration before the first line that uses the name.
Naming and Commenting Habits
Declare one variable per statement unless the variables are closely related. This keeps unrelated variables separate and makes each one easier to read.
Add a comment to a declaration when the variable's purpose or other useful information would help a human reader understand the program.
- each declaration introduces one variable on its own line
- each comment states the purpose of the variable for a human reader
- both names are legal identifiers and neither is a reserved word
Practice Your Declarations
Decide whether each candidate is a legal identifier: total_1, Total, my var, 2fast, class. For each illegal name, state which rule it breaks.
Hints
- Check the first character before anything else.
- Look for spaces anywhere in the name.
- Ask whether the word is one the language reserves for its own use.
Write a declaration for a variable that stores a player's score, using a legal identifier and a comment that explains its purpose. Then state which line must come first in the code: the declaration or the first use of the variable.
Hints
- Use the pattern type name, then identifier, then semicolon.
- The declaration must come before any line that uses the name.
Key Takeaways
- 1An identifier is a sequence of one or more characters used to name classes, variables, and subroutines. It must begin with a letter or underscore, consist only of letters, digits, and underscores, and cannot contain spaces.
- 2Reserved words such as class, int, and return are outside the set of legal identifiers, and uppercase and lowercase letters produce distinct identifiers.
- 3The simple declaration pattern is a type name, then a variable name, then a semicolon. Executing a declaration associates the name with memory set aside for the variable.
- 4A variable must be declared before it can be used, so declarations come before the first line that refers to the name.
- 5Declare one variable per statement when the variables are unrelated, and comment declarations when a human reader needs to know the purpose.
At a glance
- An identifier is a sequence of one or more characters used to name classes, variables, and subroutines, and it must begin with a letter or underscore, containing only letters, digits, and underscores.
- Reserved words are not legal identifiers, and uppercase and lowercase letters create distinct identifiers.
- A declaration uses a type name followed by a variable name and a semicolon, and executing it associates the name with memory for the variable.
- A variable must be declared before it is used, so the declaration must come earlier in the code than any use.
- Declare one variable per statement for unrelated variables, and comment declarations when the purpose would help a reader.