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:
- 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.
- A reserved keyword cannot be used as identifier name e.g. class, abstract, public, private, new, short, package, int, this, void etc.
- Special characters and whitespaces are not allowed in naming identifiers e.g. %number or number 1, are invalid identifiers.
- 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.
- 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.
- Package name should contain only lower case letters e.g. lang, awt, net, etc.
- A class name should be name of an entity or noun that start with uppercase letter e.g. Car, House, Products, Food etc.
- Interface should be named against attribute of a known or adjective that start with uppercase letter e.g. ActionListener, Collection etc.
- Method name should represent some very or action and start with lowercase letter e.g. moveForward(), toString(), etc.
- Variable or field name must be meaningful & should contain only lowercase letters e.g. sum, number, result etc.
- 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.
One Reply to “Identifiers and Naming Conventions – Java Tutorial”