⏱️ 6 min read • Beginner Level • Lesson 13
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.
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.
| 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 |
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 |
firstName and firstname are different names.
Package names should be written in lowercase letters. In real projects, package names often use a reversed domain name style.
package com.prowessapps.java;
class Student {
// class body
}
Class and interface names should use PascalCase. Class names are usually nouns, while interface names can be nouns or adjectives depending on their purpose.
class StudentDetails {
// class name uses PascalCase
}
interface Printable {
// interface name also uses PascalCase
}
Method and variable names should use camelCase. Method names usually represent actions, so they often start with verbs.
class Employee {
String firstName;
int totalMarks;
void calculateSalary() {
System.out.println("Salary calculated");
}
}
Constants are usually declared using static final and written in uppercase letters.
Enum constants are also written in uppercase letters.
class AppConfig {
static final int MAX_USERS = 100;
static final double PI = 3.14159;
}
enum Status {
ACTIVE,
INACTIVE,
PENDING
}
The following example shows package, class, variable, method, constant, and enum naming styles together.
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
NamingConventionExample is a class name written in PascalCase.MAX_MARKS is a constant written in uppercase letters.studentName and obtainedMarks are variables written in camelCase.displayResult() is a method name written in camelCase.student is an object reference variable written in lowercase/camelCase.studentDetails.DisplayResult().maxMarks.a, x, or data when meaningful names are better.🧠 Test your understanding with a quick quiz
Topic: Naming-conventions | Language: Java
🎉 Great job! Continue learning Java step by step.
Mark this lesson as completed to track your learning progress. To keep track across devices, please login and save your learning progress.
Discussion
Ask questions, share suggestions, or discuss this lesson.