When choosing names for your identifiers, such as class, package, variable, constant, method, etc., you should adhere to the Java naming standard.
It is not required to do so, though. Convention rather than the rule is how it is known. Several Java communities, such as Sun Microsystems and Netscape, have recommended these norms.
The Java naming convention is used for all of the classes, interfaces, packages, methods, and fields in the Java programming language. Failure to adhere to these norms could result in muddled or incorrect code.
Advantage of Naming Conventions in Java
You can make your code easier to read for yourself and other programmers by utilizing standard Java naming conventions. A Java program’s readability is crucial. It suggests that figuring out what the code does takes less time.
Naming Conventions of the Different Identifiers
The following table shows the popular conventions used for the different identifiers.
Identifiers Type | Naming Rules | Example |
Class | It should start with the uppercase letter. It should be a noun such as Color, Button, System, Thread, etc. Use appropriate words, instead of acronyms. | public class Employee { //code snippet } |
Interface | It should start with the uppercase letter. It should be an adjective such as Runnable, Remote, or Action Listener. Use appropriate words, instead of acronyms. | interface Printable { //code snippet } |
Method | It should start with a lowercase letter. It should be a verb such as main(), print(), or println(). If the name contains multiple words, start it with a lowercase letter followed by an uppercase letter such as actionPerformed(). | class Employee { // method void draw() { //code snippet } } |
Variable | It should start with a lowercase letter such as id, or name. It should not start with special characters like & (ampersand), $ (dollar), and _ (underscore). If the name contains multiple words, start it with a lowercase letter followed by an uppercase letter such as the first name, and last name. Avoid using one-character variables such as x, y, z. | class Employee { // variable int id; //code snippet } |
Package | It should be a lowercase letter such as java, or lang. If the name contains multiple words, it should be separated by dots (.) such as java. util, java. lang. | //package com. javatpoint; class Employee { //code snippet } |
Constant | It should be in uppercase letters such as RED, YELLOW. If the name contains multiple words, it should be separated by an underscore(_) such as MAX_PRIORITY. It may contain digits but not as the first letter. | class Employee { //constant static final int MIN_AGE = 18; //code snippet } |
CamelCase in Java naming conventions
Java uses camel-case syntax to name classes, interfaces, methods, and variables.
If the name contains two words, the second word will always begin with an uppercase letter, such as actionPerformed(), first name, ActionEvent, ActionListener, and so on.