☕ Java

Java Naming Conventions

Java naming conventions aren't enforced by the compiler — your code runs fine if you ignore them. But they're followed universally across the Java ecosystem because consistent naming makes code readable, maintainable, and instantly recognizable. Here's the complete guide.

Why Naming Conventions Matter

Java's naming conventions are not syntax rules — the compiler doesn't enforce them. But they are universally followed because the entire Java ecosystem — the JDK itself, every major framework, every open-source library — uses them consistently. When everyone follows the same conventions, you can look at an identifier and immediately know what kind of thing it is: a class, a variable, a constant, a method, a package. Reading unfamiliar code becomes dramatically faster. Code reviews are cleaner. Onboarding new team members is easier. The conventions below come from Sun Microsystems' original Java Code Conventions document, still the authoritative reference used throughout the industry.

Classes and Interfaces — UpperCamelCase

Class and interface names use UpperCamelCase (also called PascalCase): every word starts with a capital letter, no underscores, no hyphens.
Java
// Classes — nouns, UpperCamelCase:
class BankAccount { }
class UserProfileService { }
class HttpRequestHandler { }
class ArrayList { }          // from the JDK

// Interfaces — adjectives or nouns, UpperCamelCase:
interface Runnable { }       // adjective — describes a capability
interface Serializable { }
interface PaymentProcessor { }
interface List { }           // from the JDK

// Abstract classes — same rules, often prefixed with Abstract:
abstract class AbstractRepository { }
abstract class BaseController { }

// Enums — same as classes:
enum DayOfWeek { }
enum TransactionStatus { }

// DON'T:
class bankAccount { }        // starts lowercase
class Bank_Account { }       // underscores
class BANKACCOUNT { }        // all caps

Methods — lowerCamelCase

Method names use lowerCamelCase: first word lowercase, subsequent words capitalized. Methods should be verbs or verb phrases — they do things.
Java
// Methods — verbs, lowerCamelCase:
void calculateTotal() { }
String getUserName() { }
boolean isActive() { }
void sendEmailNotification() { }

// Getters and setters follow a strict convention:
private String name;

public String getName() { return name; }        // getter: get + FieldName
public void setName(String name) { this.name = name; } // setter: set + FieldName

// Boolean getters use is instead of get:
private boolean active;
public boolean isActive() { return active; }    // NOT getActive()

// DON'T:
void CalculateTotal() { }    // starts uppercase (looks like a constructor)
void calculate_total() { }   // underscores
void CALCULATETOTAL() { }    // all caps

Variables — lowerCamelCase

Variable names use lowerCamelCase. They should be short but meaningful — long enough to convey intent, short enough to type comfortably. Single-letter names are acceptable only in loops and tiny local scopes.
Java
// Local variables:
int count = 0;
String firstName = "Alice";
double accountBalance = 1500.00;
List<String> userNames = new ArrayList<>();

// Loop variables — single letters are fine here:
for (int i = 0; i < 10; i++) { }
for (String s : list) { }

// Parameters — same rules as local variables:
public void createUser(String userName, int userAge) { }

// Instance fields:
private String email;
private int loginAttempts;
private LocalDate createdAt;

// DON'T:
int Count = 0;           // starts uppercase
String first_name;       // underscore
double AccountBalance;   // uppercase start
int x1, x2, x3;         // meaningless names outside loops

Constants — UPPER_SNAKE_CASE

Constants — static final fields — use UPPER_SNAKE_CASE: all uppercase letters, words separated by underscores. This makes constants visually distinct from variables at a glance.
Java
// Constants:
static final int MAX_RETRY_COUNT = 3;
static final String DEFAULT_CURRENCY = "USD";
static final double TAX_RATE = 0.18;
static final int HTTP_OK = 200;
static final String BASE_URL = "https://api.example.com";

// Enum values follow the same UPPER_SNAKE_CASE convention:
enum Status {
    PENDING,
    IN_PROGRESS,
    COMPLETED,
    CANCELLED
}

// DON'T:
static final int maxRetryCount = 3;    // looks like a variable
static final int MaxRetryCount = 3;    // UpperCamelCase is for classes
static final int max_retry_count = 3;  // lowercase snake_case

Packages — lowercase, dot-separated

Package names are all lowercase, dot-separated, and typically follow a reverse domain name structure. No underscores, no hyphens, no uppercase — ever.
Java
// Package naming — reverse domain + project + module:
package com.google.gson;
package org.apache.commons.lang3;
package com.example.myapp.service;
package com.example.myapp.repository;
package com.example.myapp.controller;

// Structure mirrors the directory layout:
// src/
// └── com/
//     └── example/
//         └── myapp/
//             ├── service/
//             ├── repository/
//             └── controller/

// DON'T:
package Com.Example.MyApp;    // uppercase
package com.example.my-app;  // hyphens
package com.example.my_app;  // underscores (technically allowed but avoided)

Quick Reference — All Conventions at a Glance

Here's the complete summary in one place:
Java
// ── Classes & Interfaces ──────────── UpperCamelCase (noun/adjective)
class OrderProcessor { }
interface Printable { }

// ── Methods ───────────────────────── lowerCamelCase (verb)
void processPayment() { }
boolean hasPermission() { }

// ── Variables & Parameters ────────── lowerCamelCase (noun)
int totalAmount = 0;
String customerName = "";

// ── Constants ─────────────────────── UPPER_SNAKE_CASE
static final int MAX_SIZE = 100;

// ── Packages ──────────────────────── all.lowercase.dotted
package com.example.service;

// ── Enums ─────────────────────────── UpperCamelCase type, UPPER_SNAKE_CASE values
enum PaymentMethod { CREDIT_CARD, NET_BANKING, UPI }

// ── Type Parameters (Generics) ─────── Single uppercase letter
class Box<T> { }
class Pair<K, V> { }
interface Comparable<T> { }