Identifiers and Naming Conventions – Java Tutorial

Identifiers in Java:

An identifier is one that identifies something. In Java, variable name, interface name, class name, method name etc. are identifiers. E.g. in example no. 1, Hello is identifier and used as class name. You should also learn about what is class, object, variable, constructor and method in Java.

Rules for giving names to identifiers in Java:

  1. Identifiers in Java are case sensitive. Upper case letters are not considered the same as lower case letters. For example, Recipe and recipe are considered different. Similarly, Hello and hello are distinct.
  2. A reserved keyword cannot be used as identifier name e.g. class, abstract, public, private, new, short, package, int, this, void etc.
  3. Special characters and whitespaces are not allowed in naming identifiers e.g. %number or number 1, are invalid identifiers.
  4. An identifier can start with $, _ or a letter and can have a digit after first letter or symbol e.g. $number, _value, sum, NaMe, na5ME, NAx9me, $name, _name.
  5. An identifier cannot start with any digit (0-9).

You may be interested in learning how to get input from user in Java.

Naming conventions in Java:

To make your code easily readable, more understandable and easy to maintain for yourself as well as other programmers, Java naming conventions must be followed. Following is the list of standard Java naming conventions.

  1. Package name should contain only lower case letters e.g. lang, awt, net, etc.
  2. A class name should be name of an entity or noun that start with uppercase letter e.g. Car, House, Products, Food etc.
  3. Interface should be named against attribute of a known or adjective that start with uppercase letter e.g. ActionListener, Collection etc.
  4. Method name should represent some very or action and start with lowercase letter e.g. moveForward(), toString(), etc.
  5. Variable or field name must be meaningful & should contain only lowercase letters e.g. sum, number, result etc.
  6. Constant can be any value that you want to keep same throughout the program. A constant name should contain only uppercase letters e.g. BASE_URL, SIZE, MAX_ATTEMPTS etc.


Leave a Reply

Your email address will not be published. Required fields are marked *