Naming conventions in Java

There is no doubt about it; no matter how good of a Java developer you are, your code will look kind of ridiculous if you don’t know the naming conventions in Java!

But what exactly are the so-called naming conventions in Java?

In this article we’ll cover the most common naming conventions in Java – which will make your code easier to read for other programmers, and even make your code look more professional.

Variable naming conventions

What convention to use when naming your variables depends on the variable type – more specifically, whether it is a final or non-final variable.

PS: Final and non-final variables are covered in this article on Java variables, but to sum it up;

Non-final variables are variables that can change during runtime, while final variables can’t change during runtime.

The general naming convention for defining non-final variables in Java is the lower camel case naming convention.

Camel case is a naming convention in which the first letter of each word in a compound word is capitalized.
Lower camel case simply means that the first word will start with a lower case character.

So, if you were to define a non-final variable for a persons home address in Java, you would simply name the variable homeAddress.
Similarly, for a persons hometown you would name the variable homeTown.

For final variables, the naming convention is quite different.
All characters should be written in upper case, and each word should be separated by a underscore (_).
So, for a final variable holding a persons home address, the variable name would be HOME_ADDRESS.
The persons hometown would be HOME_TOWN.

  • Non-final variables: Use the lower camel case naming convention.
  • Final variables: Let all characters be upper case, and use an underscore as the separator.

Java method naming convention

The naming conventions for methods in Java is generally the same as for variables.
When naming Java methods, you do not have to worry about the access level of the method, nor whether the method is static or not.

The naming convention for Java methods is always lower camel case.
All methods should start with a lower case character, and the proceeding words should start with an upper case character.

private void getInput() {}

Note that if the method is final, you should still use lower camel case.

Java class & interface naming conventions

Naming classes and interfaces are also dead simple. You simply use the upper camel case.

As with Java methods, there’s no difference whether the class is final or static. You also don’t have to worry about the access modifier.

public class TestClass {}

Java enum naming conventions

In Java,  all enums are actually classes, and should follow the same naming pattern – upper camel case.
Instances of the enum are constants, and should follow the same naming convention as for final variables – upper case, and an underscore as the separator.

Java generics naming conventions

The naming convention for Java generics is to always name the type parameters with a single upper case letter.

  • E – Element (used extensively by the Java Collections Framework)
  • K – Key
  • N – Number
  • T – Type
  • V – Value
  • S,U,V etc. – 2nd, 3rd, 4th types

That’s pretty much all there is to it!

Happy coding 😉

 

Tutorials will only get you so far..
If you wish to expand your Java skill-set and become an advanced Java developer, I personally recommend the Comprehensive Java Course by Edureka.
You can get 25% with the coupon code LIMITED25 😉

Leave a Reply

Your email address will not be published. Required fields are marked *