Naming Conventions in Java
In Java, naming conventions are a set of guidelines that make code more readable, consistent, and easier to maintain. They are not enforced by the Java language itself but are widely followed to ensure uniformity across projects. Below are the commonly accepted naming conventions in Java:
1. Class and Interface Names 🚫
Convention: PascalCase
(also known as UpperCamelCase
)
Explanation: Each word starts with an uppercase letter, and there are no underscores or hyphens.
Examples: OurClass
, EmployeeDetails
, DataStructure
Interface names often describe an action or a characteristic, such as Runnable
, Serializable
, Parcelable
.
2. Method Names 💠
Convention: camelCase
Explanation: Starts with a lowercase letter, and each subsequent word starts with an uppercase letter. The method name typically reflects an action.
Examples: calculateTotal()
, findEmployeeByName()
, driveCar()
3. Variable and Field Names 🌐
Convention: camelCase
Explanation: Follows the same rules as method names, starting with a lowercase letter and using uppercase for each subsequent word.