The guidelines for naming conventions in Java provide a set of rules that should be adhered to when selecting names for identifiers like class, package, variable, constant, method, and more.
However, adherence to these conventions is not mandatory, making them more of a guideline than a strict rule. Various Java communities, including Sun Microsystems and Netscape, recommend following these conventions.
The Java programming language adheres to a naming convention for its classes, interfaces, packages, methods, and fields. Deviating from these conventions can lead to confusion or result in incorrect code.
Advantage of Naming Conventions in Java
By using standard Java naming conventions, you make your code easier to read for yourself and other programmers. Readability of a Java program is very important. It indicates that less time is spent figuring out what the code does.
- Enhanced Maintainability: Consistent naming conventions make it easier to maintain and update the code. Developers can quickly understand the purpose and usage of different components, reducing the time and effort required for modifications and debugging.
- Improved Collaboration: Adhering to naming conventions when working in a team ensures that all team members can understand and contribute to the codebase more efficiently. It also reduces the learning curve for new developers and facilitates smoother code reviews and pair programming sessions.
- Reduced Errors: Clear and consistent naming helps prevent errors that can arise from misinterpreting the role of variables, methods, or classes. It reduces the likelihood of introducing bugs due to misunderstandings about what a piece of code is supposed to do.
- Code Consistency: Naming conventions promote a uniform coding style across the project, making the codebase look coherent and professional. This uniformity is crucial for large projects where multiple developers are contributing code over an extended period.
- Better Integration with Development Tools: Many development tools, such as IDEs and static code analysers, are designed to work well with standard naming conventions. When these conventions are followed, these tools can provide better code completion, refactoring, and error detection.
Java Naming Conventions Rules for Different Identifiers
Below is a table displaying the commonly used conventions for various identifiers.
| Identifiers Type | Naming Rules | Examples |
|---|---|---|
| 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 classEmployee{//code snippet} |
| Interface | It should start with the uppercase letter.It should be an adjective such as Runnable, Remote, ActionListener.Use appropriate words, instead of acronyms. | interfacePrintable{//code snippet} |
| Method | It should start with lowercase letter.It should be a verb such as main(), print(), println().If the name contains multiple words, start it with a lowercase letter followed by an uppercase letter such as actionPerformed(). | class Employee{// methodvoiddraw(){//code snippet}} |
| Variable | It should start with a lowercase letter such as id, name.It should not start with the special characters like & (ampersand), $ (dollar), _ (underscore).If the name contains multiple words, start it with the lowercase letter followed by an uppercase letter such as firstName, lastName.Avoid using one-character variables such as x, y, z. | class Employee{// variableintid;//code snippet} |
| Package | It should be a lowercase letter such as java, lang.If the name contains multiple words, it should be separated by dots (.) such as java.util, java.lang. | //packagepackagecom.example;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 MAXPRIORITY.It may contain digits but not as the first letter. | class Employee{//constantstatic final intMIN_AGE= 18;//code snippet} |
CamelCase in Java Naming Conventions
In Java, the convention for naming classes, interfaces, methods, and variables is camel-case syntax.
When a name consists of two words, the convention is for the second word to begin with a capital letter. Examples of this include actionPerformed, firstName, ActionEvent, ActionListener, and so on.
Enhancing the readability of code is achieved by effectively separating the components of a name. Let's delve into a comprehensive analysis of the utilization of camelCase across different scenarios in Java:
- Classes and Interfaces: PascalCase (Upper CamelCase): In this convention, the initial letter of each word is capitalized. It is specifically employed for naming classes and interfaces.
Illustrations: ClickEvent, ClickListener, WorkerInformation, ManagerOfAccounts.
- Functions and Data: CamelCase (Initial CamelCase): The initial term begins with a lowercase character, and every following term starts with an uppercase character. This format is applied for labeling functions and data.
Illustrations of method names in programming include actionPerformed, firstName, calculateInterest, and accountBalance.
Readability and Consistency
Employing camelCase for naming variables enhances readability and understanding. It also offers visual hints regarding the identifier's purpose and organization. This approach contributes to code cleanliness by eliminating underscores and simplifying name intricacy.
Example of Naming Convention
Example
public class Main {
public static void main(String[] args)
{
int var=98; //as per naming convention variable name in lower case (var)
System.out.println("Initial value of variable is: " + var);
}
}
Output:
Initial value of variable is: 98
In the code snippet provided, we have appropriately named all identifiers according to the naming conventions.
In Java, it is essential to use lowercase for all keywords. To declare a class as public, we utilize the keywords 'class' and 'public'. The initial letter of the class name should be uppercase, hence we have named our class 'Main'.
In the following code snippet, various Java keywords such as public, static, and void are utilized. The method is named starting with a lowercase letter; hence, main serves as the entry point for the Java application. Within the main function, arguments of type String are accepted.
In the following line, a variable named var of integer type has been declared and initialized using lowercase letters.
Once more, in the subsequent declaration, we have adhered to naming convention guidelines. The initial letter of "System" is capitalized as it represents a class name. Conversely, "out" starts with a lowercase letter as it signifies an object. Additionally, "println" is written in lowercase since it denotes a method.