refresh java logo

Identifier Naming Convention

Identifiers are basically the names of variables, methods, classes, packages and interfaces. The name of variable identifiers must be written according to the rules and conventions given by the programming language. Rules are mandatory to follow while conventions are optional, but it's good to follow them. Like every programming language, java has it's own set of rules and conventions that should be followed while writing the name of an identifier. These rules and conventions can be summarized as follows.

  • Every variable name should start with either alphabets, underscore ( _ ) or dollar ( $ ) symbol. It should not start with number or any other special symbols. However the convention is to always begin the variable names with a letter, not "$" or "_". Conventionally the dollar sign should never be used at all. Variable names like age, _age, $age are valid names in java.
  • Variable names are case-sensitive. Names like age, Age, aGe, agE are different variables. These names can be used together in same program.
  • Spaces are not allowed in the variable names. For eg. int bike speed = 100 is not a valid declaration. It should be int bikespeed = 100 or int bike_speed = 100 etc.
  • Other characters apart from first character can be alphabets, numbers, $, or _ characters. For eg. user_age, a12$_ge, _$1age2 are valid names of variables.
  • When choosing a name for your variables, use full words instead of short forms, doing so will make your code easier to read and understand. Names like age, height and speed are more easy to understand than using short versions like a, h and s. This is a convention to use.
  • The variable name you choose must not be a keyword or reserved word of java. Java has defined some keywords like int, double, float, char, if, else etc. These keywords can not be used as your variable names. Java Keywords List table given below shows a list of all keywords in java.
  • Variable name should always exist in the left hand side of assignment operator. int height = 5 is correct while 5 = int height is incorrect.
  • One more convention is, if the name you choose consists of only one word, use that word in all lower case letters. If it consists of more than one word, use first letter of each subsequent word as capital letter. The variable names like userName, minHeight and maxHeight are good examples of this convention.

What if I missed a rule while writing the name of my variable ?

If it's a rule not convention, the compiler will catch it and will throw a compilation error while compiling the program.

Java Keywords List

Below table shows the list of java keywords that programmers can not use for naming their variables, methods, classes etc.

abstract continue for new switch
assert default goto package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp volatile
const float native super while

Apart from these keywords, you can also not use true, false, null as name of your identifier since these are literals.

What is literals in java ?

Any constant value that can be assigned to an integer, floating, character, boolean, string data type or can be used in an expressions are called literals. For example 10, 20.5f, 'a', true, "refresh" etc are literals.

Can I use java keywords in upper case as my variable names ?

Yes you can definitely use, as keywords in java are case-sensitive but that's not a good programming style. If you want you can use them along with other words like intVal, floatVal, charValue, doubleVar etc are valid identifier names.

Java Program of Naming Variables

 class NamingConvention
   {
      public static void main(String [] args)
         {
           byte $num1=20, _num2 = 10, num3$_ = 25;
           System.out.println("$num1 = "+ $num1 +", _num2 = "+ _num2 +", num3$_ = "+ num3$_);
           byte age=20, Age = 30, agE = 40;  
           System.out.println("age = "+ age +", Age = "+ Age +", agE = "+ agE);
           int For = 15, If = 30, Byte = 50;
           System.out.println("For = "+ For +", If = "+ If +", Byte = "+ Byte);                 
           int bike_speed = 50;
           System.out.println("bike_speed = "+ bike_speed);         
           int minSpeed = 10, maxSpeed = 80;           
           System.out.println("minSpeed = "+ minSpeed +", maxSpeed = "+ maxSpeed);         
         }
   }   	

Output:

$num1 = 20, _num2 = 10, num3$_ = 25
age = 20, Age = 30, agE = 40
For = 15, If = 30, Byte = 50
bike_speed = 50
minSpeed = 10, maxSpeed = 80

Following are some incorrect variable naming which will result in compile time error.

 int 2num1 = 40, 12num = 10; // digits not allowed as first character
 int @num = 40, &num = 10; // only $ and _ are allowed as first character
 int bike speed = 30; // Space can not be used in variable name
 int byte = 20, float = 30, for = 40; // keywords can not be used as variable names   
★★★
  • Writing a meaningful name for variables is a good practice, good variable names tells you, your team-mates what information is stored inside that variable.
  • Dollar($) Sign and Underscore(_) can be used as first character but it's not good programming style to use it as first characters.
  • If a variable needs to be used inside a local computation or in for, while, do while loops, we can prefer names like i, j, k etc.