Java Naming Conventions
In Java programming, following proper naming conventions improves code readability and maintainability. Below are the capitalization rules for different Java components:
1. Class Names
-
Rule: Use PascalCase (UpperCamelCase), where the first letter of each word is capitalized.
-
Example:
2. Interface Names
-
Rule: Follow PascalCase, similar to class names.
-
Example:
3. Method Names
-
Rule: Use camelCase (lowerCamelCase), where the first letter is lowercase, and subsequent words are capitalized.
-
Example:
4. Variable Names
-
Rule: Follow camelCase, similar to method names.
-
Example:
5. Constant Names
-
Rule: Use UPPER_SNAKE_CASE (all uppercase letters with underscores separating words).
-
Example:
6. Enum Names
-
Rule: Use PascalCase for enum names and UPPER_SNAKE_CASE for enum values.
-
Example:
7. Package Names
-
Rule: Use all lowercase letters, typically following a reversed domain name structure.
-
Example:
8. Generic Type Parameters
-
Rule: Use a single uppercase letter, such as
T
(Type),E
(Element),K
(Key),V
(Value). -
Example:
9. XML and JSON Field Names
-
Rule:
-
JSON properties typically use camelCase.
-
XML tags vary but often follow lowercase or PascalCase conventions.
-
Example:
10. Database Table and Column Names
-
Rule:
-
Table names typically use snake_case (lowercase with underscores).
-
Column names also follow snake_case.
-
Example:
CREATE TABLE user_account (
user_id INT PRIMARY KEY,
user_name VARCHAR(255),
email_address VARCHAR(255)
);
Summary Table
Component | Naming Convention | Example |
---|---|---|
Class Names | PascalCase (UpperCamelCase) | UserManager |
Interface Names | PascalCase (UpperCamelCase) | DataProcessor |
Method Names | camelCase (lowerCamelCase) | getUserName() |
Variable Names | camelCase (lowerCamelCase) | userName |
Constant Names | UPPER_SNAKE_CASE | MAX_USERS |
Enum Names | PascalCase (UpperCamelCase) | OrderStatus |
Enum Values | UPPER_SNAKE_CASE | PENDING, PROCESSING |
Package Names | all lowercase | com.example.app |
Generic Types | Single uppercase letter | T, K, V |
JSON Fields | camelCase (lowerCamelCase) | "userName" |
Database Table Names | snake_case (lowercase with underscores) | user_account |
Database Column Names | snake_case (lowercase with underscores) | user_name |