01 / 20
Aa

Java Naming
Conventions

Learn the standard rules for naming variables, methods, classes, and constants in Java: camelCase, PascalCase, and more.

Java

Why Naming Conventions Matter

Java naming conventions are agreed-upon rules that every Java developer follows. They make code readable, predictable, and professional. When you see a name like calculateTotal, you instantly know it is a method. When you see MAX_SIZE, you know it is a constant.

The Four Key Styles

camelCase
First word lowercase, each subsequent word capitalized. Used for variables and methods.
PascalCase
Every word capitalized, including the first. Used for classes and interfaces.
UPPER_SNAKE_CASE
All uppercase letters with underscores between words. Used for constants.
lowercase
All lowercase, no separators. Used for packages.

Where Each Style Applies

package com.myapp.utils class StudentRecord (PascalCase) static final int MAX_CREDITS = 120; (UPPER_SNAKE_CASE) String studentName = "Alice"; (camelCase) public double calculateGPA (int totalPoints) { double gradeAverage = totalPoints / 40.0; return gradeAverage; } (camelCase)

camelCase for Variables and Methods

camelCase begins with a lowercase letter. Each new word after the first is capitalized with no spaces or underscores. It looks like a camel's humps.

// Variables
int studentAge = 17;
String firstName = "Maria";
double totalScore = 94.5;
boolean isEnrolled = true;

// Methods
public void printReport() { ... }
public int getMaxScore() { ... }

PascalCase for Classes

PascalCase (also called UpperCamelCase) capitalizes the first letter of every word, including the first. It is used exclusively for class names and interface names.

// Classes
public class StudentRecord { ... }
public class BankAccount { ... }
public class ArrayListSorter { ... }

// Interfaces
public interface Comparable { ... }
public interface ActionListener { ... }

UPPER_SNAKE_CASE for Constants

Constants: values that never change after initialization. Declared with static final and named in all uppercase with underscores separating words.

public static final int MAX_STUDENTS = 30;
public static final double TAX_RATE = 0.08;
public static final String DEFAULT_NAME = "Unknown";
public static final int DAYS_IN_WEEK = 7;

The Core Naming Rules

1
Names are case-sensitive. score, Score, and SCORE are three different identifiers.
2
Start with a letter, underscore, or dollar sign. Never start with a digit. 2ndPlace is invalid.
3
No spaces or special characters. Use camelCase or underscores to separate words instead.
4
No reserved keywords. You cannot name a variable class, int, or return.
5
Be descriptive. Prefer studentCount over sc. Clarity beats brevity.

camelCase vs. PascalCase

camelCase first letter lowercase s tudent N ame g et T otal S core For: variables, methods, parameters PascalCase first letter UPPERCASE S tudent R ecord B ank A ccount For: classes, interfaces vs

How to Name Things Step by Step

1
Identify what you are naming. Is it a variable, method, class, constant, or package?
2
Choose a descriptive name using one or more English words that communicate purpose.
3
Apply the correct casing: camelCase for variables/methods, PascalCase for classes, UPPER_SNAKE_CASE for constants.
4
Verify: Does it start with a letter? Is it not a reserved word? Is it readable by a teammate?
5
Be consistent. If one method uses getScore, do not name another Fetch_Grade.

Example 1: Naming a Variable

Scenario: You need a variable that stores the number of students in a class.

1
Identify: This is a variable (it stores data that might change).
2
Describe: "number of students" becomes the words: number, of, students.
3
Apply camelCase: numberOfStudents (first word lowercase, rest capitalized).
int numberOfStudents = 25;

Example 2: A Complete Class

Scenario: Build a class representing a bank account with a balance and deposit method.

public class BankAccount {

  // Constant (UPPER_SNAKE_CASE)
  public static final double MIN_BALANCE = 0.0;

  // Instance variable (camelCase)
  private double accountBalance;

  // Method (camelCase)
  public void depositFunds(double amount) {
    accountBalance += amount;
  }
}

Example 3: Fixing Bad Names

Identify the naming violations and correct each one.

Bad Name Problem Fixed Name
Student_name Underscore in variable; mixed case studentName
bankaccount Class should be PascalCase BankAccount
maxSpeed Constant should be UPPER_SNAKE_CASE MAX_SPEED
x Not descriptive coordinateX
2ndAttempt Starts with a digit secondAttempt

Common Mistakes

✕ Using Underscores in Variables
Writing student_name instead of studentName. Java uses camelCase, not snake_case (which is Python's convention).
✕ Lowercase Class Names
Writing class bankAccount instead of class BankAccount. Classes must start with an uppercase letter.
✕ camelCase for Constants
Writing maxRetries for a static final field. Constants must be in UPPER_SNAKE_CASE: MAX_RETRIES.
✕ Single-Letter Names
Using a, x, t outside of small loops. Always prefer descriptive names like area, xCoordinate, temperature.

Complete Reference Table

Element Convention Example Part of Speech
Variable camelCase totalScore Noun
Method camelCase calculateGPA Verb
Class PascalCase StudentRecord Noun
Interface PascalCase Comparable Adjective
Constant UPPER_SNAKE_CASE MAX_SIZE Noun
Package lowercase com.myapp Noun

Question 1 of 5

Which naming convention should be used for a Java class that represents a student's report card?

Question 2 of 5

A constant storing the maximum number of allowed attempts should be named:

Question 3 of 5

Which of the following is a valid Java variable name that follows standard conventions?

Question 4 of 5

A developer writes: public class shoppingCart { }. What is wrong with this declaration?

Question 5 of 5

Given the code below, which line violates Java naming conventions?