Java Naming Conventions - Classes, Methods, Variables and Constants

⏱️ 6 min read • Beginner Level • Lesson 13

Lesson 13 of 124 of Java Tutorial
You have completed this lesson

Completed on . You can revise this lesson or continue to the next topic.

Java naming conventions are recommended rules for naming identifiers such as packages, classes, interfaces, methods, variables, and constants. They make code easier to read, understand, and maintain.

Before learning naming conventions, you should understand Java identifiers and Java variables. After this lesson, continue with Java literals.


What are Naming Conventions in Java?

Naming conventions are style guidelines that developers follow while naming program elements. They are not compulsory rules like Java syntax, but they are considered good programming practice.

Simple Meaning: Naming convention means choosing meaningful and consistent names for classes, methods, variables, constants, packages, and interfaces.

Why are Naming Conventions Important?

  • They make Java programs easier to read.
  • They help developers understand the purpose of an identifier.
  • They improve code consistency in team projects.
  • They make code easier to maintain and debug.
  • They help differentiate classes, methods, variables, constants, and packages.

Java Naming Convention Rules

Identifier Naming Rule Examples
Package Use lowercase letters. java.util, com.prowessapps.tutorial
Class Use PascalCase. Class names should usually be nouns. Student, EmployeeDetails, StringBuffer
Interface Use PascalCase like class names. Runnable, ActionListener
Method Use camelCase. Method names should usually be verbs. main(), print(), calculateSalary()
Variable Use camelCase. Start with lowercase letter. firstName, orderNumber, totalMarks
Constant Use uppercase letters with underscores. MAX_VALUE, PI, DEFAULT_LIMIT
Enum Constant Use uppercase letters. MONDAY, ACTIVE, PENDING

Camel Case and Pascal Case in Java

Java commonly uses camelCase and PascalCase naming styles.

Style Meaning Example
camelCase First word starts lowercase, next words start uppercase. firstName, calculateTotal()
PascalCase Every word starts with an uppercase letter. StudentDetails, ActionListener
UPPER_CASE All letters uppercase, words separated by underscore. MAX_PRIORITY, MIN_VALUE
Remember: Java identifiers are case-sensitive. firstName and firstname are different names.

Package Naming Convention

Package names should be written in lowercase letters. In real projects, package names often use a reversed domain name style.

PackageExample.java
package com.prowessapps.java;

class Student {
    // class body
}

Class and Interface Naming Convention

Class and interface names should use PascalCase. Class names are usually nouns, while interface names can be nouns or adjectives depending on their purpose.

ClassInterfaceNaming.java
class StudentDetails {
    // class name uses PascalCase
}

interface Printable {
    // interface name also uses PascalCase
}

Method and Variable Naming Convention

Method and variable names should use camelCase. Method names usually represent actions, so they often start with verbs.

MethodVariableNaming.java
class Employee {
    String firstName;
    int totalMarks;

    void calculateSalary() {
        System.out.println("Salary calculated");
    }
}

Constant and Enum Naming Convention

Constants are usually declared using static final and written in uppercase letters. Enum constants are also written in uppercase letters.

ConstantNaming.java
class AppConfig {
    static final int MAX_USERS = 100;
    static final double PI = 3.14159;
}

enum Status {
    ACTIVE,
    INACTIVE,
    PENDING
}

Complete Example of Java Naming Conventions

The following example shows package, class, variable, method, constant, and enum naming styles together.

NamingConventionExample.java
Copy Try Download
class NamingConventionExample {
    static final int MAX_MARKS = 100;

    String studentName;
    int obtainedMarks;

    void displayResult() {
        System.out.println("Student Name: " + studentName);
        System.out.println("Marks: " + obtainedMarks + "/" + MAX_MARKS);
    }

    public static void main(String[] args) {
        NamingConventionExample student = new NamingConventionExample();
        student.studentName = "Ayan";
        student.obtainedMarks = 85;
        student.displayResult();
    }
}

Output:

Student Name: Ayan
Marks: 85/100

Explanation

  1. NamingConventionExample is a class name written in PascalCase.
  2. MAX_MARKS is a constant written in uppercase letters.
  3. studentName and obtainedMarks are variables written in camelCase.
  4. displayResult() is a method name written in camelCase.
  5. student is an object reference variable written in lowercase/camelCase.

Common Mistakes in Java Naming

  • Starting class names with lowercase letters, like studentDetails.
  • Starting method names with uppercase letters, like DisplayResult().
  • Writing constants in lowercase, like maxMarks.
  • Using unclear names like a, x, or data when meaningful names are better.
  • Using spaces or special characters in identifiers.
  • Using Java keywords as identifiers.
Summary:
  • Naming conventions make Java code readable and maintainable.
  • Packages should be lowercase.
  • Classes and interfaces should use PascalCase.
  • Methods and variables should use camelCase.
  • Constants and enum constants should be uppercase.
  • Naming conventions are not compulsory, but they are strongly recommended.

Interview Questions ⭐

Naming conventions in Java are recommended rules for naming identifiers such as packages, classes, interfaces, methods, variables, and constants.

No, Java naming conventions are not compulsory, but they are strongly recommended because they make code easier to read and maintain.

Class names should be written in PascalCase, where every word starts with an uppercase letter, such as StudentDetails or EmployeeRecord.

Method names should be written in camelCase and should usually represent actions, such as calculateSalary(), displayResult(), or printMessage().

Variable names should be written in camelCase and should start with a lowercase letter, such as firstName, totalMarks, or orderNumber.

Constant names should be written in uppercase letters, and multiple words should be separated by underscores, such as MAX_VALUE or DEFAULT_LIMIT.

Package names should be written in lowercase letters, such as java.util or com.prowessapps.java.

camelCase is a naming style where the first word starts with lowercase and each next word starts with uppercase, such as firstName or calculateTotal().

PascalCase is a naming style where every word starts with an uppercase letter, such as StudentDetails or ActionListener.

No, Java keywords cannot be used as identifiers because they are reserved words with predefined meanings.

Next step: Learn Java Literals

🚀 Continue to Java Literals →

🧠 Test your understanding with a quick quiz



🚀 Quick Knowledge Check

Topic: Naming-conventions | Language: Java

Question 1 of 15
Q1. How should constants be named in Java?
Q2. Which of the following is a correctly named Java variable?
Q3. Why should developers follow Java naming conventions?
Q4. Which of the following is a correctly named Java constant?
Q5. What is PascalCase?
Q6. Which naming style is recommended for Java class names?
Q7. How should package names be written in Java?
Q8. What is camelCase?
Q9. Which naming style is recommended for Java method names?
Q10. What are naming conventions in Java?
Q11. Which of the following is a valid package naming style?
Q12. Which of the following is a correctly named Java class?
Q13. Which of the following is a correctly named Java method?
Q14. Which naming style is recommended for Java variables?
Q15. Can Java keywords be used as variable names?

🎉 Great job! Continue learning Java step by step.

Discussion

Ask questions, share suggestions, or discuss this lesson.

Please login or create an account to join the discussion and save your learning activity.

Loading comments...

Have you completed this lesson?

Mark this lesson as completed to track your learning progress. To keep track across devices, please login and save your learning progress.

Not completed yet.