Java Naming Conventions

Java naming conventions are sort of guidelines that application programmers are expected to follow to produce consistent and readable code throughout the application. If teams do not follow these conventions, they may collectively write an application code that is hard to read and difficult to understand.

Always try to give descriptive and concise names to all variables. As a result, any programmer will enjoy your code for a long time. Experienced programmers put a lot of care into naming to make their programs easy to understand.

Java heavily uses Camel Case notations for naming the methods, variables, etc., and TitleCase notations for classes and interfaces.

There are two generally accepted Java conventions that are used all over the world:

Let’s understand some commonly used and popular naming conventions in detail with examples.

1. Naming Packages

Package names must be a group of words starting with all lowercase domain names (e.g. com, org, net, etc). Subsequent parts of the package name may be different according to an organization’s own internal naming conventions.

package com.howtodoinjava.webapp.controller;

package com.company.myapplication.web.controller;

package com.google.search.common;

2. Naming Classes

In Java, class names generally should be nouns, in title-case with the first letter of each separate word capitalized. e.g.

public class ArrayList {}

public class Employee {}

public class Record {}

public class Identity {}

3. Naming Interfaces

In Java, interface names, generally, should be adjectives. Interfaces should be in the title case with the first letter of each separate word capitalized. In some cases, interfaces can be nouns as well when they present a family of classes e.g. List and Map.

public interface Serializable {}

public interface Clonable {}

public interface Iterable {}

public interface List {}

4. Naming Methods

Methods always should be verbs. They represent action and the method name should clearly state the action they perform. The method name can be single or 2-3 words as needed to clearly represent the action. Words should be in camel case notation.

public Long getId() {}

public void remove(Object o) {}

public Object update(Object o) {}

public Report getReportById(Long id) {}

public Report getReportByName(String name) {}

5. Naming Variables

All instance, static, and method parameter variable names should be in camel case notation. They should be short and enough to describe their purpose. Temporary variables can be a single character e.g. the counter in the loops.

Java has some rules for naming variables:

  • names are case-sensitive;
  • a name can include Unicode letters, digits, and two special characters ($_);
  • a name cannot start with a digit;
  • a name must not be a keyword (classstaticint, etc. are illegal names).
  • whitespaces are not allowed in the name of a variable.

Here are some valid names of variables:

public Long id;

public EmployeeDao employeeDao;

private Properties properties;

for (int i = 0; i < list.size(); i++) {

}

And here are some invalid ones:

@ab, 1c, !ab, class

Since Java 9, the single character _ is an invalid name for a variable, but _a and __ (a double _ ) are legal names.

Always choose a name that makes sense, e.g. score makes more sense than s, although they are both valid.

6. Constant Naming Conventions

Java constants should be all UPPERCASE where words are separated by underscore character (“_”). Make sure to use the final modifier with constant variables.

public final String SECURITY_TOKEN = "...";

public final int INITIAL_SIZE = 16;

public final Integer MAX_SIZE = Integer.MAX;

7. Naming Generic Types

Generic type parameter names should be uppercase single letters. The letter 'T' for type is typically recommended. In JDK classes, E is used for collection elements, S is used for service loaders, and K and V are used for map keys and values.

public interface Map <K,V> {}

public interface List<E> extends Collection<E> {}

Iterator<E> iterator() {}

8. Naming Enums

Similar to class constants, enumeration names should be all uppercase letters.

enum Direction {NORTH, EAST, SOUTH, WEST}

9. Naming Annotations

Annotation names follow title case notation. They can be adjectives, verbs, or nouns based on the requirements.

public @interface FunctionalInterface {}

public @interface Deprecated {}

public @interface Documented {}

public @Async Documented {

public @Test Documented {}

10. Conclusion

In this post, we discussed the naming conventions in Java to be followed for consistent writing of code which makes the code more readable and maintainable.

Naming conventions are probably the first best practice to follow while writing clean code in any programming language.

Happy Learning !!

Comments

Subscribe
Notify of
guest
4 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode