
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
\\nWhy we should follow the naming conventions in Java?\\n
The Java team and developer community suggest us to follow naming conventions. They are just conventions and are not mandatory but help in writing Java programs that are more understandable and easier to read.
For example, class names should generally be nouns, and interface names should be adjectives. Additionally, capitalize the first letter of each separate word. The names used for classes, variables, and methods are called identifiers.
Need to Follow Java Naming Conventions
Following are the reasons to follow naming conventions:
- Multiple developers work on the same project simultaneously. Names that follow naming standards reduce the effort required for other developers to read and understand the source code.
- It enables code reviews to focus on checking logic, code optimization, and other important issues rather than arguing over syntax and naming standards.
- To enable code quality review tools to focus their reporting mainly on significant issues other than syntax and style preferences.
The reasons given above not only help you write better Java code but also improve coding in other programming languages.
Naming Conventions for Different Identifiers
The naming conventions for different Java identifiers are as follows:
- Package Naming Convention
- Interface Naming Convention
- Class Naming Convention
- Method Naming Convention
- Variables Naming Convention
- Constants Naming Convention
Package Naming Convention
The Java packages name should be in all lower case. It's common to use the reverse domain name as a prefix to ensure uniqueness.
Example
An example of package in Java is given below:
package com.tutorialspoint;
Interface Naming Convention
Interface names in Java should start with an uppercase letter and typically use camelCase. Use a descriptive noun or adjective that represents the behavior or functionality the interface provides.
Example
If you have an interface that defines the color, you could name it as Color:
interface Color { // some statements }
Class Naming Convention
All the words of a class name should start with an uppercase character. Try to keep the name simple and use nouns to describe the class's purpose.
Example
Name of the class should be similar to given example:
class TutorialsPointClass { }
Method Naming Convention
In method naming convention, we should use verbs that describes the action the method performs. Method names should start with the first letter lowercase and the first letter of each internal word capitalized (camelCase).
Example
If the method you are writing prints some kind of message, name it printMessage as shown below:
class TutorialsPointClass { void printMessage() { // some code } }
Variable Naming Convention
In variable naming convention, the first word should be in lower case and the internal words start with capital letters. Variable names should not start with underscore _ or dollar sign $ characters.
Example
Some example of variable names are:
class TutorialsPointClass { int rollNum; String firstName; String lastName; }
Constant Naming Convention
When naming constants, use all capital letters with underscores(_) to separate words. We can also use the digits but the name should not start with the digits.
Example
An example of constant is given below:
class TutorialsPointClass { public static final int MAX_SCORE = 100; }