Java Naming Convention
π· What is a Naming Convention?
Naming conventions are a set of rules and guidelines used to name identifiers such as classes, methods, variables, and packages in a programming language. In Java, following naming conventions improves readability, maintainability, and ensures code is standardized and consistent.
π· Why Naming Conventions Matter?
-
Makes the code more understandable to other developers.
-
Reduces confusion and errors.
-
Follows industry standards and improves collaboration.
-
Required for professional and clean code design.
π· Java Naming Convention Rules
1. Class Names
-
Should be nouns, in PascalCase (each word starts with a capital letter).
-
Example:
Student
,BankAccount
,CarEngine
2. Interface Names
-
Use PascalCase, just like classes.
-
Should usually describe capability or behavior.
-
Example:
Runnable
,Serializable
,Readable
3. Method Names
-
Should be verbs or verb phrases.
-
Use camelCase (first word lowercase, others capitalized).
-
Example:
getData()
,calculateInterest()
,printReport()
4. Variable Names
-
Should be short yet meaningful.
-
Use camelCase.
-
Example:
age
,studentName
,totalAmount
5. Constant Names
-
Use uppercase letters with underscores between words.
-
Example:
PI
,MAX_SPEED
,DATABASE_URL
6. Package Names
-
Always use lowercase letters to avoid conflicts.
-
Often follow the reverse domain name convention.
-
Example:
com.companyname.projectname
π Best Practices:
-
Always use meaningful names.
-
Donβt use special characters or spaces.
-
Avoid using single-letter names like
x
,y
(except in loops). -
Follow consistent formatting throughout the project.
β Quick Summary:
Identifier Type | Convention | Example |
---|---|---|
Class | PascalCase | EmployeeDetails |
Method | camelCase | getSalary() |
Variable | camelCase | employeeAge |
Constant | UPPER_CASE | MAX_SIZE |
Interface | PascalCase | Readable |
Package | lowercase | com.myapp.student |