Java Variable (Identifier) Naming Tutorial

Java, like any computer language, has naming conventions and rules for legal names of primitive variables. In future tutorials you will learn about things such as: classes and interfaces, methods, and constants. The names of classes, interfaces, methods, variables, and constants are collectively known as identifiers. This tutorial will teach you the rules of valid names for your identifiers.



Identifiers

Rule 1 · First Character of the Identifier

The first character of the identifier must begin with one of the following: the letters a-z, an underscore _, or a currency character $. Identifiers CANNOT begin with a number from 0-9.

Rule 2 · Remaining Characters of the Identifier

After the first character, the identifier can contain the numbers 0-9, the letters a-z, an underscore _, or a currency character $. There is no limitation on the length of an identifier.

Rule 3 · Java Keywords

Keywords are reserved words in the Java programming language. Java has 50 keywords and you cannot use them for the names of your identifiers. The Java keywords are: abstract · assert · boolean · break · byte · case · catch · char · class · const · continue · default · do · double · else · enum · extends · final · finally · float · for · goto · if · implements · import · instanceof · int · interface · long · native · new · package · private · protected · public · return · short · static · strictfp · super · switch · synchronized · this · throw · throws · transient · try · void · volatile · while

Rule 4 · case-sensitive

Identifiers are case-sensitive. You can name an identifier FIRSTNAME and another identifier firstName, they will be two completely separate identifiers.

Naming Conventions

CamelCase is the convention ofWritingCompoundWordsWithNoSpaces and AnInitialUppercaseLetterForEachWord. Using camelCase is not specifically a Java rule, so you are not required to adhere by it. However, it is a best practice and I highly recommend utilizing the naming conventions listed below.

Variable naming convention

The first letter of the Identifier should begin with a lowercase letter and all remaining words should begin with an uppercase letter. Examples: firstName · lastName · streetNumber · myHairColor.

Method naming convention

The first letter of the Identifier should begin with a lowercase letter and all remaining words should begin with an uppercase letter. The name should also be in the form of a verbNoun that describes the purpose of the method. Examples: doUpload · getFileName · addPoints · exitMap.

Class naming convention

All of the first letters of every word should be in uppercase. Class names should be nouns. Examples: Boeing787 · MonthyReport · Car · Engine.

Interface naming convention

All of the first letters of every word should be in uppercase. Examples: Playable · Runnable · FunctionalInterfaceTripleIt.

Constants naming convention

Variables marked as static and final are known as Java constants. Identifiers for Java constants should be words that are entirely in uppercase that are separated by an underscore _. Examples: MAX_WIDTH · DEFAULT_COLOR · MIN_AGE .


Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md Identifiers
C:\Java>cd Identifiers
C:\Java\Identifiers>Notepad Identifiers.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


/*
    Demonstrate proper and improper names for identifiers
*/
class Identifiers {
    public static void main(String args[]) {
        int fourGSpeed; 
        int 4GSpeed; // Not legal! Begins with a number
        int g4Speed; 
        int #highScore; //Not legal! Begins with a prohibited character
        int _highScore; 
        int $highScore;
        int high@Score; // Not legal! Contains a prohibited character
        int goto; // Not legal! goto is a Java keyword
        int $__$;
        int _7$_;
        
        System.out.println("The first character of the identifier must begin with one of the following:");
        System.out.println("the letters a-z, an underscore _, or a currency character $.");
        System.out.println(); // empty println - what will that do?
        System.out.println("After the first character, the identifier can contain the following: ");
        System.out.println("the numbers 0-9, the letters a-z, an underscore _, or a currency character $.");
    }
}

Now switch back to the command prompt (CMD) and type in javac Identifiers.java and press Enter.
Now type in java Identifiers and press Enter.


C:\Java\Identifiers>javac Identifiers.java
C:\Java\Identifiers>java Identifiers
The first character of the identifier must begin with one of the following:
the letters a-z, an underscore _, or a currency character $.

After the first character, the identifier can contain the following:
the numbers 0-9, the letters a-z, an underscore _, or a currency character $.

Final thoughts

Please take some time to study the naming rules and conventions for your identifiers. They are a fundamental key to writing good quality Java code. Your co-workers will appreciate it, and with practice and time, it will become second nature.


Tutorials