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

public class Student {
// class body
}

2. Interface Names

  • Use PascalCase, just like classes.

  • Should usually describe capability or behavior.

  • Example: Runnable, Serializable, Readable

public interface Printable {
void print();
}

3. Method Names

  • Should be verbs or verb phrases.

  • Use camelCase (first word lowercase, others capitalized).

  • Example: getData(), calculateInterest(), printReport()

public void calculateSalary() {
// method logic
}

4. Variable Names

  • Should be short yet meaningful.

  • Use camelCase.

  • Example: age, studentName, totalAmount

int studentAge = 21;
String studentName = "Chad";

5. Constant Names

  • Use uppercase letters with underscores between words.

  • Example: PI, MAX_SPEED, DATABASE_URL

public static final double PI = 3.14159;

6. Package Names

  • Always use lowercase letters to avoid conflicts.

  • Often follow the reverse domain name convention.

  • Example: com.companyname.projectname

package com.example.studentapp;

πŸ“ 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

Related Posts