Java variable naming rules

Variables in Java Programming

[one_half last="no"]Variable in Java[/one_half]Variables in Java Programming is something that can be changed, such as a characteristic or value. If we consider programming concept then we can say that variable is something which can store a value. It is container for storing a value.Variable name may be given by programmer.

Some Rules of Variable Naming Convention :

  1. Variable names are case-sensitive.
  2. A variable’s name can be any legal identifier.
  3. It can contain Unicode letter,Digits and Two Special Characters such as Underscore and dollar Sign.
  4. Length of Variable name can be any number.
  5. Its necessary to use Alphabet at the start (however we can use underscore , but do not use it )
  6. Some auto generated variables may contain ‘$‘ sign. But try to avoid using Dollar Sign.
  7. White space is not permitted.
  8. Special Characters are not allowed.
  9. Digit at start is not allowed.
  10. Subsequent characters may be letters, digits, dollar signs, or underscore characters.
  11. Variable name must not be a keyword or reserved word.
[468x60]

Some Standard Conventions Used :

1. Never Use Dollar Or Underscore as First Letter

  • Dollar Sign and Underscore as First Character in Variable name is allowed but still its not good programming Style to use it as First Character.
  • Generally we use Underscore in “Constant Value” variable.
static final int TOTAL_NUM = 10;

static final int MAX_COUNT = 20;

*Final is nothing but constant value in Java (In c we use const)

2. Capitalization of First Character of Second Word

  • If variable name contain two words then write first letter of second word in Capital Case.
  • If variable name contain single word then write that word in small case.
[468x60]

Example : Multiple Words in Variable Name

sumOfNumber

firstNumber

skinColor

Example : Single Word in Variable Name

sum

first

last