Skip to content

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:

public class UserManager { }
public class OrderService { }

2. Interface Names

  • Rule: Follow PascalCase, similar to class names.

  • Example:

public interface DataProcessor { }
public interface UserRepository { }

3. Method Names

  • Rule: Use camelCase (lowerCamelCase), where the first letter is lowercase, and subsequent words are capitalized.

  • Example:

public void processOrder() { }
public String getUserName() { }

4. Variable Names

  • Rule: Follow camelCase, similar to method names.

  • Example:

String userName;
int orderCount;
boolean isActive;

5. Constant Names

  • Rule: Use UPPER_SNAKE_CASE (all uppercase letters with underscores separating words).

  • Example:

public static final int MAX_USERS = 100;
public static final String DEFAULT_LANGUAGE = "English";

6. Enum Names

  • Rule: Use PascalCase for enum names and UPPER_SNAKE_CASE for enum values.

  • Example:

public enum OrderStatus {
    PENDING, PROCESSING, COMPLETED, CANCELED
}

7. Package Names

  • Rule: Use all lowercase letters, typically following a reversed domain name structure.

  • Example:

package com.example.usermanagement;
package org.mycompany.services;

8. Generic Type Parameters

  • Rule: Use a single uppercase letter, such as T (Type), E (Element), K (Key), V (Value).

  • Example:

public class Box<T> { }
public class Pair<K, V> { }

9. XML and JSON Field Names

  • Rule:

  • JSON properties typically use camelCase.

  • XML tags vary but often follow lowercase or PascalCase conventions.

  • Example:

{ "userName": "JohnDoe", "orderCount": 5 }
<user>
    <userName>JohnDoe</userName>
    <orderCount>5</orderCount>
</user>

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