Java Naming Conventions

Naming conventions are a set of rules that dictate how variables, methods, classes, and packages should be named. Consistently following these conventions can help make your code more readable and easier to understand for other programmers. In this tutorial, we’ll go over the naming conventions for various elements in Java programming.

Classes

Class names should always begin with a capital letter and be in camel case, with the first letter of each word capitalized. For example:

public class Car {}

Interfaces

Interface names should also begin with a capital letter and be in camel case. To distinguish them from classes, it’s recommended to use a noun or noun phrase for interface names. For example:

public interface Vehicle {}

Methods

Method names should always begin with a lowercase letter and be in camel case, with the first letter of each subsequent word capitalized. For example:

public void driveCar() {}

Variables

Variable names should always begin with a lowercase letter and be in camel case. For example:

int numCars = 4;

Constants should be written in uppercase, with words separated by underscores. For example:

public static final int MAX_NUM_CARS = 10;

Packages

Package names should be in all lowercase letters, with words separated by dots. For example:

package com.example.car;