Java Syntax · Lesson 2/9
22%
⏱ 5 min read Modified: 2026-07-04

Best Practices for Naming Java Identifiers: Classes, Methods, Constants & Packages

In this article, we will discuss recommendations for naming identifiers in Java.

Remember that Java is case-sensitive: for example, myVar, MyVar, and MYVAR are three different identifiers. When naming classes, methods, variables, and other identifiers, it is recommended to follow these rules:

1. Classes and Interfaces

  • Class or interface names should start with an uppercase letter. For example: Cat, Dog.
  • If the name consists of multiple words, each word should start with an uppercase letter (UpperCamelCase format, also called PascalCase) - PrintReader.
  • Class names are usually nouns, such as Cat, Exam, PrintReader. Names like Eatable and DoSomething are not ideal for classes but are better suited for interfaces.
  • Interface names should be adjectives, such as Comparable, Iterable, Navigable.

2. Methods

  • A method name should start with a lowercase letter and follow lowerCamelCase rules.
  • Method names should combine verbs and nouns, for example: getName, doJob, setLastName — because methods perform actions.

3. Variables

  • A variable name should start with a lowercase letter and follow lowerCamelCase principles.
  • Use short, clear names that indicate the purpose of the variable, such as firstName, buttonHeight.

4. Constants

  • In Java, constants are declared using the static and final keywords.
  • Constant names should be in uppercase letters, with words separated by underscores, e.g., MAX_WEIGHT.

5. Packages

  • Package names should use only lowercase letters.
  • For commercial projects, it is common to use the reversed domain name of the organization; the package usually starts with com, followed by the company name and project name. After that, packages are typically named by functionality. For example, for ExamClouds we could create the package com.examclouds.javacore with sub-packages like lesson1, lesson2, and so on. There are also other top-level domains: org (non-commercial), net, io.

Frequently Asked Questions

Can I use the $ and _ symbols in identifiers?

Technically yes: an identifier may contain the dollar sign $ and the underscore _. But there are caveats. By convention, $ is reserved for code generated by tools and the compiler, so it's not recommended in ordinary code. A single underscore _ on its own (as a variable name) has been forbidden since Java 9 — it causes a compilation error. An underscore inside a name (for example, MAX_WEIGHT) is allowed and is actively used in constant names.

What's the difference between camelCase and PascalCase, and where is each used?

These are two ways of writing compound names. In lowerCamelCase the first word starts with a lowercase letter and the rest are capitalized: getName, firstName. This is used for methods and variables. In UpperCamelCase (also called PascalCase) every word starts with a capital letter, including the first: PrintReader, ArrayList. This is used for classes and interfaces. The only difference is the first letter, but it tells the reader whether they're looking at a class or a variable.

Why are constants written in uppercase?

It's a convention that immediately distinguishes a constant from a regular variable. If a name is written in uppercase with words separated by underscores (MAX_WEIGHT, PI), the reader knows the value is declared with static final and won't change while the program runs. The compiler doesn't require this style — the code compiles with an ordinary name too — but following the convention makes the code clearer.

Can I name variables using non-English letters?

Technically yes: Java uses Unicode, so an identifier can contain letters from almost any language, including Cyrillic or others. However, it's not recommended. The common practice is to name identifiers in English using Latin letters: this keeps the code understandable to other developers, compatible with libraries, and safe across different encodings and tools.

Comments

Please log in or register to have a possibility to add comment.

‹ Previous lesson Next lesson ›