Table of Contents
Introduction
In Java, class and instance variable names, as well as input parameter names are typically either nouns our noun phrases. Generally speaking, parameter and variable names should be ASCII letters, numbers an underscore or a dollar sign. However, the dollar sign is discouraged and the underscore is typically only expected for constants. Beyond those rules, the formatting varies a bit. Here, I am describing the Google Style Guide’s Naming Conventions, as that is what I have setup for the Java 101 project.
Input Parameter Names
A method’s input parameters are typically written in lowerCamelCase format. The first letter starts with a lower case letter. Subsequent words start with an upper case letter. After the first letter, the rest of each word is typically lower case. Occasionally, a number will be used. No spaces or other characters are included. Whether the final keyword is present or not, the same lowerCamelCase format is used.
For example:
public void setName(final String newName) {
...
public void add(int value1, int value2) {
...
Local, Instance and Static Variable Names
As you think about variable names, please keep in mind that you may look at this code in the future with groggy eyes. If you are called out to troubleshoot code at 2am, will your variable name help you quickly understand what should be stored within a variable? Did you remember to include units? That is another easy source of confusion and bugs – forgetting to include the expected unit of measure in the variable name and then making an incorrect assumption later in the code.
Constant Variable Names
Static (class) variables that also have the final keyword are considered constants. They typically use UPPER_SNAKE_CASE formatting, with only upper case letters and underscores between words. Sometimes a number will be included. No spaces or other special characters are expected. This is sometimes also called SCREAMING_SNAKE_CASE.
For example:
private static final int MIN_CACHE_TIME_TO_LIVE_SECONDS = 20;
private static final int MAX_CACHE_TIME_TO_LIVE_SECONDS = 300;
Non-Constant Variable Names
The same rules apply whether we are talking about:
- local variables within a method,
- instance variables within a class, or
- class (static) variables,
In all of these scenarios, the lowerCamelCase formatting rules apply. Again, the first word starts with a lower case letter and subsequent words start with an upper case letter, with the remaining letters in the word appearing in lower case. Occasionally, numbers will be included. However, spaces, underscores and other such special characters are not allowed.
For example, a local variable within a method might be defined like:
String name = "Sally";
Loop Counters
When working in a simple loop, it may be easier to use a single-character as a variable name for the loop counter. Typically, you will want to limit this type of behavior to just 2 levels of looping, as it can quickly get confusing. Often, in this situation, the variable names i and j will be what is used. For example:
for (int i = 0; i < outerSize; i++) {
for (int j = 0; j < innerSize; j++) {
...
}
}
Summary
When a Java variable name appears in all caps with underscores between words (AKA UPPER_SNAKE_CASE format), it is understood to be a constant. Other Java parameters and variables typically use the lowerCamelCase format, in which the first letter is lower case and the first letter of subsequent words is capitalized. Simple loop variables may have single-character names, such as i or j. However, in general, we want Java variable names to be either nouns or noun phrases that describe the content they contain. If applicable, a variable name should also indicate what is the expected unit of measure for the variable’s value.

Leave a comment