Java naming conventions are a standard pattern for writing the identifier names. These identifiers are classes, variables, interfaces, methods, constants, and more. Although the naming conventions are not strict rules that must be followed, so it is known as conventions, not as a rule. We may or may not follow these patterns; it will not affect our Java program. But, these conventions are approved and recommended by several Java communities such as Sun Microsystems and Netscape.
Java naming conventions are followed to reduce the erroneous and confusion in Java code. So that all the classes, variables, interfaces, packages, and fields have their standard pattern in which they are written.
If i told you to imagine the world without names, then you must be thinking about this weird question. So, everything has a name to recognize its availability. In the programming world, it will become more critical.
So be careful while naming the names of the identifiers. It will be going to tell the world what you are doing in your program. Try to choose standard and straightforward names that can be identified by everyone. It will increase your code readability.
Before proceeding further, particulars conventions. Let’s see how many types of cases are there.
Types of Cases
There are four types of cases that are commonly used:
- Mixed case
Let’s see each of them with an example:
We are much familiar with lower case notations. In this notation, all the letters are written in small letters such as identifier object, class, camel, and many more.
In the Uppercase notation, all the letters are written in Capital letters such as CLASS, OBJECT, VARIABLE, VAR_VALUE, and more.
In the Camelcase notation, the first letter of any word is written in capital letters. All other words are written in small letters—for example, CamelCase, HelloWorld, VariableValue, and more.
The Mixed case notation is quite similar to camelcase notation; the only difference is the first letter. In mixed-case notation, the first letter starts with a small letter—for example, javaClass, javaMethod, chhatrapatiShahuJiMaharaj, and many more.
Java Naming Conventions Rules
The Following are some set of rules which is followed by Java programming language:
- Variable names are case- sensitive in Java.
- Variable names can have letters, digits, and two special characters, which is underscore (_) and dollar ($).
- The name can not start with a number; it must start with an alphabet letter.
- White spaces and special characters (except underscore and $) are not allowed in a name.
- Variable name must be a keyword (later we will discuss in Java keywords)
- The special characters can be used for separating the words.
There are various patterns and rules for each identifier. See the below table for complete naming conventions for each identifier.
|Classes||CamelCase||The class name should be meaningful as it deals with th real-world objects.||Student, Car, CustomerAccounts, EmployeeSalary, etc.|
|Variables||mixedCase||Variable represents the value of objects.||studentName, carName, customerName, salary, etc.|
|Constants||UPPERCASE||Constants have a fixed value.||DEFAULT_VALUE, MAX_VALUE, SUM, etc.|
|Methods||mixedCase||The method contains an action, so try to choose verb names.||calculateSalary, displayValue, getAmount etc.|
|Packages||lowercase||Try to choose a meaningful name. There are many predefined packages in Java.||package awt, package newproject, package myclaculator etc.|
|Interfaces||CamelCase||The interface contains an action of the class.||ActionListener, Runnable, IEnumerable|