Java Naming
Conventions
Learn the standard rules for naming variables, methods, classes, and constants in Java: camelCase, PascalCase, and more.
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
Where Each Style Applies
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.
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.
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 double TAX_RATE = 0.08;
public static final String DEFAULT_NAME = "Unknown";
public static final int DAYS_IN_WEEK = 7;
The Core Naming Rules
camelCase vs. PascalCase
How to Name Things Step by Step
Example 1: Naming a Variable
Scenario: You need a variable that stores the number of students in a class.
Example 2: A Complete Class
Scenario: Build a class representing a bank account with a balance and deposit method.
// 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
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?
Line 1: public class GradeCalculator {
Line 2: private int Total_Points;
Line 3: public static final int MAX_SCORE = 100;
Line 4: public double getAverage() { return 0.0; }
Line 5: }