Course topics

By WebNest Studio Engineering Team

Java - Core Tutorial

Java Naming Conventions

Java naming conventions are not enforced by the compiler — code that ignores them still compiles and runs — but they are followed almost universally in professional Java code and in the JDK itself. Following them makes your code instantly readable to any other Java developer, since the casing of an identifier signals what kind of thing it is before you even see its declaration.

Why it matters

Conventions let any Java developer read unfamiliar code and immediately tell a class from a method from a constant. The compiler does not enforce them, but teams do: code that ignores them fails review, confuses tools, and marks its author as new to the language.

Watch: Java Naming Conventions, in Detail

Prefer to watch? This video walks through how Java names classes, methods, variables, constants and packages, and why the conventions matter.

Watch on YouTube

Conventions by Identifier Type

Each category of identifier in Java has its own established casing style. Departing from these conventions is legal but considered poor practice and is flagged by linters and code reviewers.

  • Classes and interfaces — PascalCase (also called UpperCamelCase): every word starts with a capital letter, no underscores. Example: BankAccount, Runnable.
  • Methods and variables — camelCase: the first word is lowercase, subsequent words are capitalized. Example: calculateTotal(), studentName.
  • Constants — UPPER_SNAKE_CASE: all uppercase letters with underscores separating words, and almost always declared static final. Example: MAX_DISCOUNT, DEFAULT_TIMEOUT.
  • Packages — all lowercase, typically the reversed domain name of the organization, with no underscores. Example: com.webnest.banking.

Why These Conventions Exist

The casing itself carries information: seeing Vehicle car = new Vehicle(); tells an experienced Java reader immediately that Vehicle is a type and car is a variable, purely from capitalization, without needing to look up either declaration. Consistent naming also avoids collisions — lowercase packages never clash visually with PascalCase class names even when a package and a class share a similar word.

Additional Naming Rules

Beyond casing style, identifiers should be descriptive rather than abbreviated (totalPrice rather than tp), and single-letter names are conventionally reserved for loop counters (i, j) or generic type parameters (T, E, K, V). Boolean variables and methods conventionally read as a question or assertion, such as isValid or hasPermission, which makes conditional code read naturally.

Enums, Generics, Getters and Acronyms

A few more cases come up in almost every real project, and the second example below shows each of them:

  • Enums — the enum type is PascalCase like any class (OrderStatus), and its constants are UPPER_SNAKE_CASE because they are fixed values (PLACED, OUT_FOR_DELIVERY).
  • Generic type parameters — a single capital letter: T for a type, E for a collection element, K and V for map keys and values, R for a return type.
  • Getters and setters — get or set plus the field name in PascalCase (getCustomerName(), setCustomerName()). A boolean getter uses is instead (isActive()). Frameworks such as Spring and Jackson find properties by these exact names.
  • Acronyms — treat them as ordinary words so the casing stays readable: HttpClient and parseJson() rather than HTTPClient and parseJSON(). The JDK is not fully consistent here (it has both HttpClient and URL), but most style guides recommend the word form for new code.

Examples

All four naming conventions applied in one small class

Java
package com.webnest.inventory; // package: all lowercase

public class InventoryItem { // class: PascalCase

    static final int MAX_QUANTITY = 1000; // constant: UPPER_SNAKE_CASE

    private String itemName; // variable: camelCase
    private int quantityInStock; // variable: camelCase

    public boolean isInStock() { // method: camelCase, reads as a question
        return quantityInStock > 0;
    }

    public static void main(String[] args) {
        InventoryItem item = new InventoryItem();
        item.itemName = "Notebook";
        item.quantityInStock = 25;
        System.out.println(item.itemName + " in stock: " + item.isInStock());
    }
}
Output
Notebook in stock: true

Enums, generics, getters and setters named the way the JDK names them

Java
import java.util.ArrayList;
import java.util.List;

public class OrderService {
    enum OrderStatus { PLACED, SHIPPED, DELIVERED } // enum type: PascalCase; constants: UPPER_SNAKE_CASE

    static class Box<T> { // generic type parameter: a single capital letter
        private final T item;
        Box(T item) { this.item = item; }
        T getItem() { return item; }
    }

    private String customerName; // field: camelCase
    private boolean active;      // boolean field: plain adjective

    public String getCustomerName() { return customerName; }      // getter: get + field name
    public void setCustomerName(String name) { customerName = name; } // setter: set + field name
    public boolean isActive() { return active; }                  // boolean getter: is + field name
    public void setActive(boolean active) { this.active = active; }

    public static void main(String[] args) {
        OrderService service = new OrderService();
        service.setCustomerName("Asha");
        service.setActive(true);

        List<OrderStatus> history = new ArrayList<>();
        history.add(OrderStatus.PLACED);
        history.add(OrderStatus.SHIPPED);

        Box<OrderStatus> latest = new Box<>(OrderStatus.SHIPPED);
        System.out.println(service.getCustomerName() + " active: " + service.isActive());
        System.out.println("Status history: " + history);
        System.out.println("Latest status: " + latest.getItem());
    }
}
Output
Asha active: true
Status history: [PLACED, SHIPPED]
Latest status: SHIPPED

Common Mistakes

  • Naming a class in camelCase (like "bankAccount") instead of PascalCase, which makes it visually indistinguishable from a variable.
  • Using ALL_CAPS for a regular mutable field instead of reserving that style for true constants declared with "static final".
  • Mixing underscores into camelCase names (like "student_Name"), combining two different conventions inconsistently.
  • Using vague single-letter or abbreviated names for fields and methods outside of loop counters or generics, which hurts readability.
  • Naming a boolean getter "getActive()" instead of "isActive()", which breaks the JavaBeans pattern that frameworks such as Spring and Jackson rely on.

Exercise

This program compiles, but every name breaks a Java naming convention. Rename the class, the constant, the method and the variables to follow the conventions. The output must not change.

Expected output: Total: 236

Java · starter code
class order_item {
    static final int taxpercent = 18;

    static int Calculate_Total(int PRICE, int Qty) {
        int sub_total = PRICE * Qty;
        return sub_total + sub_total * taxpercent / 100;
    }

    public static void main(String[] args) {
        System.out.println("Total: " + Calculate_Total(100, 2));
    }
}
Show hints
  • Classes use PascalCase, methods and variables use camelCase, and constants use UPPER_SNAKE_CASE.
  • Rename one identifier at a time and update every place it is used.
Show solution
Java · solution
class OrderItem {
    static final int TAX_PERCENT = 18;

    static int calculateTotal(int price, int quantity) {
        int subTotal = price * quantity;
        return subTotal + subTotal * TAX_PERCENT / 100;
    }

    public static void main(String[] args) {
        System.out.println("Total: " + calculateTotal(100, 2));
    }
}

Quick quiz

1. Which name follows the convention for a class?

  • A. CustomerOrder
  • B. customerOrder
  • C. customer_order
  • D. CUSTOMER_ORDER
Show answer

A. Class names use PascalCase: every word starts with a capital letter and there are no underscores.

2. Which name follows the convention for a method?

  • A. CalculateTotal
  • B. calculateTotal
  • C. calculate_total
  • D. CALCULATETOTAL
Show answer

B. Methods use camelCase and usually start with a verb.

3. Which package name follows the convention?

  • A. com.Webnest.Billing
  • B. Com_Webnest_Billing
  • C. com.webnest.billing
  • D. COM.WEBNEST.BILLING
Show answer

C. Package names are all lower case, normally starting with the reversed domain name of the organisation.

Interview questions

Why follow naming conventions if the compiler does not enforce them?

Show answer

Because code is read by people and tools far more often than it is written. Consistent names tell a reader at a glance whether something is a type, a method, a variable or a constant. Frameworks also rely on them: libraries that work with JavaBeans expect getters and setters named getName and setName, and will not find properties that are named differently.

Key Points to Remember

  • Classes and interfaces use PascalCase; methods and variables use camelCase.
  • Constants (static final fields) use UPPER_SNAKE_CASE; packages are all lowercase.
  • Enum constants use UPPER_SNAKE_CASE, generic type parameters are single capital letters, and getters/setters follow get, set and is + the field name.
  • These rules are conventions, not compiler-enforced syntax, but ignoring them harms readability for every other Java developer.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Runs plain Java 17. Frameworks such as Spring, and databases, are not available here. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering