Home Java Java-tips Data Variables Variable Names

Ask Questions?

View Latest Questions



Variable Names
Posted on: July 26, 2006 at 12:00 AM
Basic variable naming conventions

Java Notes

Variable Names

Basic variable naming conventions

Choosing good names is probably the single most important factor in making programs readable -- or unreadable. It's worth spending some time to get good names. When I come back to a program of mine and find unhelpful names, the first refactoring I apply is to replace bad names with good names. Why would I choose bad names in the first place? When everything is fresh in your mind, you don't need the extra help of good names so its easy to choose an unhelpful name. Later, or to another reader, it's often obvious.

Sun's Code Conventions for the Java Programming Language (java.sun.com/docs/codeconv/) is the most widely used set of Java coding guidelines. Altho the organization you work for may have have different standards in some aspects, these variable naming conventions are almost always used. Here is what they have to say about variable names.

"Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters. Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.

Variable names should be short yet meaningful. The choice of a variable name should be mnemonic - that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary "throwaway" variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters."


  • Special case: Domain names. It's important to use variable names which correspond to the problem domain. For example, two-dimensional coordinates are traditionally named x and y. These short names would be acceptable because they correspond to the same names in the problem domain. Because x and y are also traditional names for algebraic variables, it might be even better to name them xCoord and yCoord.
  • Special case: Booleans. Boolean variables are often named beginning with the prefix is, for example, isClean, isOpen, etc.
  • Special case: Loop variables. The meaningless short variable names i and j are so commonly used as iteration variable names, that it is permissible to do so. Do not use i and j for other purposes.
  • Special case: n is commonly used for the number of elements to be processed.
  • Special case: Method return value. If the return value of a method is stored in a variable, the name result is commonly used.
  • Plural names are often used for arrays or collections of multiple values.

Differentiating by Lifetime (Local, Instance, and Class variables)

Hints about about a variable's lifetime can be quite useful. Sun gives no guidance on this, and as a result, there are many different styles, including the most popular -- no naming distinction.

  • Instance variables are often prefixed with one of the following:
    • m_ - "Member" is another term for instance (C++ origin).
    • _ - Underscore is sometimes used in C++ and carried over to Java. This violates Sun's standard naming conventions, but it is used sometimes. Making it a suffix puts it back into the Sun naming convention. You will find it in some of my code, as well as other conventions.
    • its
    • my
    • this. may be used to qualify instance variables. The problem with this is that it isn't enforced by the compiler. On the other hand, the advantage is that it can also easily be used with functions, whose names can't be changed when they are inherited.
  • Class - Static variables are sometimes prefixed with
    • the

Differentiating type - Hungarian notation


Copyleft 2004 Fred Swartz MIT License