What's in a Name: Java Naming Conventions

DZone 's Guide to

What's in a Name: Java Naming Conventions

What's in a name?

· Java Zone ·
Free Resource

Imagine a world without names. What would happen if nothing in this world had a name? This article would have been a very different story. Oh sorry, we wouldn’t even be able to tell the story. What about if we had weird names? Then what will aliens think about us? So, basically, everything is in the name, especially when it comes to programming languages.

Programming is a story; every character and scene needs to have a name that properly tells you what it is and what it does. You should be careful while naming things: You don’t want to make your code hard to read or debug. Remember: You are the creator here. You can name anything from babies, stars, trees, and mountains, almost anything (almost). Paying little attention to this feature will make your code more readable, easy-to-debug, and whatnot. And you should be able to look at it after months and be able to understand it without any trouble.

Now, let us discuss naming conventions in Java. We talk about different aspects of Java programming like package, class, variable, and functions.


The base package name should have your company’s domain name in reverse order, plus your project name without any extra character — all in lowercase letters. You can add the version name as well, in the end, as shown in the following example:



The class name must be a noun. You can use CamelCase, but the first letter should be capitalized, like HashMap. It should tell you what kind of function and/or variable you can expect from it as well. It must be the Specialized (explained in Interface block). For example:

class Student{}
class ArrayList{}
class HashMap{}
class ComputerEngineer{}


Interface names should be an adjective. Sometimes, it can bea noun as well like List or Map. Like classes, it should be in CamelCase, but the first letter should be capitalized. It must be generalized, for example, ComputerEngineer. If it were to implement any interface, which would have been implemented? Engineer, right? So, the Engineer is the general term, but the ComputerEngineer is the specialized one.

interface Cloneable{}
interface AutoCloseable{}
interface PreparedStatement{}
interface Engineer{}

Variables or Fields

Variables or fields must be nouns, short, meaningful, and should tell you what kind of value they hold and are used for. They should be written in camelCase. And do not start them with an underscore(‘_’) or dollar sign ‘$’ characters. Additionally, do not write one character variable. You can use one character for temporary variables in a loop or something similar, commonly used characters are i, j, k, m, and n for integers and c for characters. I mean it's up to you; it's only temporary. But it's even better if you attach the unit as well. For a boolean value, it makes it more of a question with a yes or no answer. Generally, ‘is’ or ‘has’ is attached at the beginning.

int countOfCustomer;
float averageInterest;
long timeInMillisecond, daysInYear; //with some unit
boolean isEngineer, isCompleted, hasSubmitted; // questions


Constant is also a big part of the program. You want to make things stay the way they are, always. So, the common practice is to make it appear as if they are not going to change. Just like a stubborn kid, you have to make them all capital. Yep! No evading of the work here.

public static final float PI = 3.14;
static int CREATED_ON_YEAR = 2019;
final String CODERSTEA_URL = "https://CodersTea.com";

Methods or Functions

Who does most of the work here? Yep, methods. They should always report to you what they are trying to do and what actions they are taking. So, they should be placed in the verb category and the name should imply what they are doing. Make it 2-3 words long. Then, again, use camelCase, as you did in variables. Usually, get and set are used to fetch and set the data to something respectively (setters and getters). For boolean, it's the same as variables.

void print(Object obj);
void remove(Obejct obj);
Object update();
int getCountOfCustomer();//getter
void setCountOfCustomer(int countOfCustomers);//setter
boolean isUserAdmin(User user);


There is not much to talk about generics. It is better to use a single capital character for it. Usually, T is used. If more than one the V is also used, Java uses E for the list element, K and V for maps, and S for service loaders. Do not use a multi-character for it. It, then, becomes hard to detect it from another class name.

public <T> void print(T t);
interface List<E>{}
class HashMap<K, V> {};

Remaining Team

There are a few other things, like enum and annotations. They are similar to classes and interfaces respectively. In enum fields, they must all be capital since they are final static by default.

enum Beverage{TEA, COFFEE};
public @interface FunctionalInterface {}
public @interface Autowired{}
enum HttpResponse {Ok, NOT_FOUND....}

The Trick

You should use proposed conventions for every program, whether it be small and simple or big and complex. There are times when you couldn’t decide the name for your class, method, or anything else. There is a trick that can help you to make a name for it that I often use:

  • Just give 5 seconds and no more than that to decide the name.
  • If the name doesn’t come to you, then just press any key on the keyboard (a-z only) for the name, like dgsjsbdjdb — just give it any unreadable name.
  • Finish up your method or class.
  • Then, think about the name again and change it from the temporary, unreadable one.
  • If it still doesn’t come to you, then divide and conquer. One of the reasons you may not be able to decide the name is that too many things are going in a single place. Just make it do one thing at a time. And name that thing along the way.
  • Repeat for all the others.

……. And that’s it for this post. I hope you enjoyed and learned something new!

classes, constant, functions, generics, interface, java, methods, naming, naming conventions, variable

Published at DZone with permission of Imran Shaikh . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}