×
☰ See All Chapters

Identifiers in Java – Rules for Java Identifiers

Java identifiers are used to identify many elements in code, including class names, method names, field names, variable names, and package names. The names you choose are called identifiers and must adhere to the following rules:

  1. Identifier cannot be any keyword or true, false, or null. 

  2. Identifier can consist of letters, digits 0–9, the underscore, or the dollar sign. Uppercase and lowercase letters are distinct. 

  3. Identifier must not begin with a digit. An identifier must start with a letter, an underscore, or a dollar sign. 

Java Identifiers Conventions

Along with above rules there are naming conventions too. These naming conventions are not mandatory to follow.  So, it is known as convention not rule. But as a good programmer we need to follow the naming conventions. These conventions are suggested by several Java communities and followed worldwide.

Element

Convention

class, interface, enum, annotation

Should start with uppercase letter. If name has more than one word follow CamelCase, means words should begin with a capital letter with no intervening spaces or punctuation.

Example : HelloWorld, ManuManjunatha

method

Should start with lowercase letter. If name has more than one word follow CamelCase.

Example : helloWorld, manuManjunatha

variable

Should start with lowercase letter. If name has more than one word follow CamelCase.

Example : helloWorld, manuManjunatha

package

All the letters should be in lowercase. Different words should be separated by dot (.) character.

Example : com.java4coding, hello.world, manu.manjunatha

constants

All the letters should be in Uppercase. Different words should be separated by Underscore (_) character.  Example : HELLO_WORLD, MANU_MANJUNATHA

 

 

 

 


All Chapters
Author