Java : Identifier Naming Conventions

What is an identifier?

Identifier is the name of components like packages, classes, interfaces, variable, methods and constants.

How to decide name of an identifier?

  1. Name should be meaningful, don’t worry about lengthy name.
  2. Use Camel case when name is lengthy (except package and Constants) to make it more understandable.
  3. Follow Naming Conventions rules while deciding name of identifiers.

What are rules for Java Naming Conventions?

Java naming conventions follow below rules:

  1. A keyword cannot be used as an identifier.
  2. Identifiers are case sensitive.
  3. Identifiers must not contain white spaces.
  4. All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an underscore (_).
  5. The name should not start with special characters like & (ampersand), $ (dollar), _ (underscore).
  6. After the first character, identifiers can have any combination of characters.
  7. For big identifiers names use Camel case.

Example of Legal Identifiers:
name, age, $salary, _amount, __1_amount.
Example of Illegal Identifiers:
123xyz, -amount.

Camel case:  It consists of compound words or phrases such that each word or abbreviation begins with a capital letter or first word with a lowercase letter, rest all with capital.

Lower Camel Case Example : hasChildren, firstName, isValidUser etc.

Upper Camel Case Example : AccountHolder, CustomerAccount, PlayingCard etc.

Java Naming Convention Identifiers
Java: Naming Convention Components

Advantage of  Naming Conventions

Java naming conventions must be followed while doing java programming so that improve maintenance and readability of  code. Because Naming conventions  :

  • Make code easily readable and understandably for themselves and for other programmers.
  • Less time of analysis , modify and fixing if any issue.

Note: These conventions are suggested by several Java communities such as Sun Microsystems and Netscape. It doesn’t forced to follow convention rule.

Naming Conventions for different Identifiers:

Package Naming Conventions

  • Name should be in lower case.
  • Make name as more understandable and know hierarchy of source code, name should be  sub divided  by dot(.).
  • Use prefix as company domain name to make it unique.
  • One of the top-level domain names, like com, edu, gov, mil, net, org. Subsequent components of the package name vary according to an organisation’s own internal naming conventions.
package com.faccingissuesonit.utilities


package com.faccingissuesonit.dao

package org.facingissuesonit.presentation

Class Naming Conventions

  • It should be a noun such as AccountHolder, Employee, Department etc.
  • It should start with the uppercase letter and long names should use camel case.
  • Use appropriate words, instead of acronyms.
class Employee


Interface Naming Conventions

  • It should be a adjective such as Runnable, Serialization, Cloneable etc.
  • It should start with the uppercase letter and long names should use camel case.
  • Use appropriate words, instead of acronyms.
interface  Account
class AccountHolder implements Account


Object/Variable Naming Conventions

  • Variable name should be short , yet meaningful.
  • Object/variable name should start from lower case letter.
  • Long names should use camel casing.
  • Avoid using variable/object name as x, y and z to make code more understandable.
  • Variable name should not start with underscore(_) or dollar($), even though both are allowed in variable/ object name.
class Account
    int accountNumber;
    double salary;
    AccountHolder accountHolder;

Methods Naming Conventions

  • Methods should be verbs, such as getEmployeeDetail() , getCurrentMonthStatement() etc.
  • It should be start with small letter and long name should use camel case.
class Engine{
      void changeGear(int newValue);
      void speedUp(int increment);
      void applyBrakes(int decrement);

Constants Naming Conventions

  • Should be all uppercase with words separated by underscores (“_”).
  • Always declared with identifiers static and final so that threat as constant.
// Some Constant variables used in predefined Float class
public static final float POSITIVE_INFINITY = 1.0f / 0.0f;
public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;
public static final float NaN = 0.0f / 0.0f;

class Employee
static final int MIN_AGE= 18;


Here you learn about :

  1. Naming Convention Guidelines
  2. Advantage of naming Convention
  3. How to categorize identifiers based on Naming Conventions like Class, interface, methods and Constants etc.