Core Java

Java Naming Conventions

In this example, we will be discussing naming code conventions for the Java Programming Language. We will talk about different identifier types and the best practices to follow while naming them in your program.

1. Introduction

As a team member of a codebase of any size (small, medium, or large), it is the responsibility of the programmer or the developer to write code that is readable, maintainable, and easy to understand.

Martin Fowler says:

Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

Java Naming Conventions

I am sure that after following the naming conventions explained in this article, you can write clean, readable, and easy to maintain code. These conventions are guidelines or recommendations which should be followed when programming in java. Following is a deep dive.

2. Java Naming Conventions

Let’s discuss various identifier types in Java and their naming conventions in detail.

2.1 Packages

Package name should be a dot (.) separated set of words, unique, consisting of lower cases ASCII letters. Underscores can also be used.

  1. If you are working for an enterprise, use the company’s internet domain name in a reversed manner. For example, for the domain it.cornell.edu, the package name should be edu.cornell.it.[mypackagename] and for cloud.oracle.com it should be com.oracle.cloud.[mypackagename].
  2. The project name may be added to the package name following Step 1. Usage of such words helps in resolving name collisions in a big enterprise across different projects/teams. For example, com.oracle.cloud.compute.[mypackagename] where the word “compute” represents the project name.
  3. Further, as per the company’s internal naming conventions, the division, the business unit, the department, or any internal name may also be used to write the subsequent components of the package name.

Package name examples

package com.google.docs.core

package com.oracle.cloud.analytics

package org.jcg.examples.java.namingconventions

2.2 Classes

  1. Use nouns in Title Case to represent Class names.
  2. The first letter of each word must be capitalized.
  3. Try to avoid acronyms and abbreviations.
  4. The class name represents an object so keep it simple and descriptive.

Class name examples

public class Car {...}

public class CarService {...}

public class CarFactory {...}

public class Driver {...}

2.3 Interfaces

  1. For interfaces, follow the same capitalization naming convention as in class names.
  2. Whenever an Interface offers a capability, end the name with “able“. For example, the Cloneable, the Serializable, and the Comparable interfaces in Java follow such code conventions.

Interface name examples

public interface Vehicle {...}

public interface VehicleRepository {...}

public interface VehicleService {...}

2.4 Methods

  1. Method names describe an action taken by an object on its state. Hence, it’s recommended to use verbs for naming methods.
  2. The first letter of the method name should be a lowercase letter and each subsequent internal word’s first letter should be capitalized.

Method name examples

public void wash(Vehicle vehicle) {...}

public Balancing calculateWheelBalancing(Tyre[] tyres) {...}

2.5 Variables

Variables can be classified into static, instance, local variables, and method parameters. Let’s discuss the naming conventions for each of them.

2.5.1. Instance and Static Variables

  1. Use nouns for instance and static variable names.
  2. Follow the same capitalization naming convention as in method names.
  3. Do not start these names with an _(underscore) or a $ (dollar sign), even though both are allowed.

2.5.2 Parameter and Local Variables

  1. The use of descriptive lowercase single word is advisable for parameter and local variable names.
  2. If you wish to use multiple words, follow the same capitalization naming convention as in method names.
  3. Temporary variable names for integers should be single characters e.g. i, j, and k.

Variable name examples

// instance & static variables

private String engineName;
public static int gears;

// parameter & local variables

	public void calculateMaximumSpeed(float engineDisplacement, int numberOfGears) {
		int maximumSpeed = 0;
		for (int i = 0; i < numberOfGears; i++) {

		}
	}

2.6 Constants

  1. Use all uppercase letters for naming constants.
  2. If there are multiple words, separate them using underscores.

Constants name examples

public static final int MAX_FILE_SIZE = 8192;

2.7 Enumeration and Annotations

  1. Follow the same naming conventions as in Class names for Enumerations. The set of choices of an enumeration should be all uppercase letters.
  2. Annotation names should also follow the Title Case notation as in Class names. Based on the requirements, use an adjective, verb, or a noun.

Enumeration and Annotation name examples

public enum Status {SUCCESS, ERROR, WARNING, PENDING , COMPLETED}

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}

2.8 Generic Types

  1. The letter ‘T’ is typically recommended for Generic type parameter names.
  2. The classes in Java Development Kit use the letter ‘E’ for collection elements, ‘S’ for service loaders, and ‘K’ and ‘V‘ in case of Maps.

Generic Types name examples

public interface Map  {
   V put(K key, V value);
   V get(Object key);
   ...
}

public interface Comparable  {
    public int compareTo(T o);
}

3. Summary

In this article, we explained in detail the naming code conventions someone should follow when programming in Java. These are one of the best coding practices that result in a clean, readable, and easy to understand code.

4. Download the source code

Download
You can download the full source code of this example here: Java Naming Conventions

Anmol Deep

Anmol Deep is a senior engineer currently working with a leading identity security company as a Web Developer. He has 8 years of programming experience in Java and related technologies (including functional programming and lambdas) , Python, SpringBoot, Restful architectures, shell scripts, and databases relational(MySQL, H2) and nosql solutions (OrientDB and MongoDB). He is passionate about researching all aspects of software development including technology, design patterns, automation, best practices, methodologies and tools, and love traveling and photography when not coding.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Anil
Anil
4 years ago

Nice and useful information !

Back to top button