Best Practices for Naming Java Identifiers: Classes, Methods, Constant

In this article, we will discuss recommendations for naming identifiers in Java.
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 (CamelCase format) - 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
- The name should start with a lowercase letter and follow CamelCase rules.
- Method names should combine verbs and nouns, for example: getName, doJob, setLastName, as methods perform actions.
3. Variables
- Variable names should start with a lowercase letter and follow CamelCase 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, the package name should start with com, followed by the company name and project name. Then, packages can be named by functionality. For example, for ExamClouds, we could create the package com.examclouds.javacore with sub-packages like lesson1, lesson2, and so on.

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