Archangel Macsika Sikademy Image
Archangel Macsika

Learn the Various Naming Conventions in Java Programming

Tutorial 8 of 9 | 4 minutes read
Learn the Various Naming Conventions in Java Programming - Sikademy

Naming conventions in Java are the set of rules and guidelines which software developers and programmers are expected to follow in order to produce written code that is not only easily readable, but also consistent throughout the application.

It also ensures that software development teams follow a particular coding conventions when working on a product together, otherwise, they may collectively write inconsistent codes that is difficult to read and understand.

Naming Conventions of Java Identifiers

You will learn the Java naming conventions to be followed for consistent writing of code which make the code more readble and maintainable.

In Java, the naming convention commonly used are:

  1. CamelCase Notations

  2. TitleCase Notations

CamelCase notation is used for naming identifiers such methods, variables etc.

TitleCase notations is used for classes and interfaces.

Let’s understand these naming conventions in detail with examples.

Java Naming Conventions for Variables

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

Some examples are: i, x, y, id, email, firstName, lastName, fullName, nameOfOrganization, dateOfBirth, amountSpent, etc.


int id;
int i;
String firstName;
String lastName;
String fullName;
String nameOfOrganization;
double amountSpent;

Java Naming Conventions for Methods

Method names should be in camelcase notation and always be verbs because they state an action. Knowing this, the method name should clearly state the action they perform and can be a single or 2-3 words as needed to clearly or represent the action.

Some examples are: getFirstName(), setFirstName(), getEmail(), deleteEmail(), remove(), update(), getInformationById(Long id), getInformationByName(String name) etc.


public Long getFirstName() {
	// Method codes
}

public Long setFirstName() {
	// Method codes
}

public Long getEmail() {
	// Method codes
}

public Long deleteEmail() {
	// Method codes
}

public void remove(Object o) {
	// Method codes
}

public Object update(Object o) {
	// Method codes
}

public Report getInformationById(Long id) {
	// Method codes
}
 
public Report getInformationByName(String name) {
	// Method codes
}

Java Naming Conventions for Classes and Constructor

In Java, class names generally should be singular nouns and should be in titlecase notation with the first letter of each separate word capitalized. Constructors in Java are predefined to have the same name as the class in which they exist.

Some examples are: Company, Employee, PartTimeEmployee, FullTimeEmployee, Record, Identity.


public class Company {
	// Class codes
}
 
public class Employee {
	// Class codes
}

public class PartTimeEmployee {
	// Class codes
}

public class FullTimeEmployee {
	// Class codes
}
 
public class Record {
	// Class codes
}
 
public class Identity {
	// Class codes
}

Java Naming Conventions for Constants

Java constants should all be in UPPERCASE where the combination of words are separated by an underscore character ("_"). Ensure to use final modifier with constant variables.

Some examples are: SECURITY_KEY, INITIAL_SIZE, MAXIMUM_SIZE etc.


public final String SECURITY_KEY = "...";
 
public final int INITIAL_SIZE = 16;
 
public final Integer MAXIMUM_SIZE = Integer.MAX;

Java Naming Conventions for Packages

The name of a Java Package must be a group of words starting with all lowercase domain name such as com, org, net etc. followed by subsequent parts of the package name which depends on the developer or an organization’s own internal naming conventions.

Some examples are below:


package com.trainingjavastudents.webapp.controller;

package com.company.applicationname.web.controller;

package com.google.search.common;

Java Naming Conventions for Interfaces

In Java, interfaces names, generally should be in titlecase with the first letter of each separate word capitalized. Names of interfaces can be verbs or adverbs when they describle an action or executing function and also nouns as well when they represent a group of classes such as List and Map.

Some examples are below:


public interface Serializable {
	// Interface code
}
 
public interface Clonable {
	// Interface code
}
 
public interface Iterable {
	// Interface code
}
 
public interface List {
	// Interface code
}

Java Naming Conventions for Enumeration

The naming convention of enumeration is Similar to class names. I should be all uppercase letters.

Some examples are NORTH, EAST, SOUTH, WEST


enum Direction {
	NORTH, EAST, SOUTH, WEST
}

Java Naming Conventions for Annotations

The naming conventions of Annotation in Java Language should follow titlecase notation. They can be adjective, verb or noun based the on requirements.

Some common examples are @Interface, @Override, @Asyn, @Test etc.


Some common examples are @Interface, @Override, @Asyn, @Test etc.

Java Naming Conventions for Generic

The parameter names for a Generic type 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.

Examples:


public interface Map  {}
 
public interface List extends Collection {}
 
Iterator iterator() {}

Tutorial Exercise

Come up with five different variables names for a standard registration form. Assign a value to each of them and print the result.
e.g firstName = "Archangel";


Enjoy this Tutorial? Please don't forget to share.