Java Naming Conventions – How To Design a Java Class

In this chapter, we are going to look at the basic principles and Java naming conventions that we should follow while writing any Java Class.

I guess you have enough idea about What is Class in Java and Object-Oriented Programming.

1. Java Naming Conventions

It’s recommended that you should follow Java naming conventions while developing any Java application to achieve readability and maintenance of Java Code.

Also, Oracle suggests writing the name of methods, variables, etc in camelCase.

What is CamelCasing?

CamelCasing is a naming convention that makes a sentence or phrase readable.

In CamelCase, multiple words have their first letter Capital and then joined together to form a single word. For Example LearnJava, MasterInCoding, WritingInCamelCase, etc.

Now let’s jump into the Java naming conventions.

1.1 Package Declaration

In Java, packages are named in all small letters and in reverse order, like we have a domain masterincoding.com then I should name my package as com.masterincoding.something.

The reverse order is recommended and the package must define the functionality of the classes it contains.
com.masterincoding
com.masterincoding.controller
com.masterincoding.data
com.masterincoding.helper
com.masterincoding.repo

1.2 Class Declaration

The first letter of any Class should be Capitalized and you can also follow the CamelCase convention.

The name of Java Class must be a noun. A Class should tell what type of implementation it is holding.

class SoftwareEngineer{}
class Student{}
class Dog{}

1.3 Interface Declaration

Interfaces name should be adjective and sometimes they could be a noun. In other words, we should always name an interface with some generalized name.

For instance, If we have a class Dog then it must implement Animal Interface. Therefore, Animal is a general term and Dog is a specialized one.

interface Engineer{}
interface Animal

1.4 Variables Declaration

Variables in Java are very important and while writing any program they should tell what kind of value they are going to hold. Therefore, their names should be very short, meaningful, and descriptive.

  • Practice writing in camelCase.
  • Never start a variable name with Dollar( ‘$’ ) and Underscore( ‘_’ ).
  • Always avoid one character variable name expect variable used in looping statements.
int countOfStudents;
long time;
boolean isCompleted;

1.5 Constants Declaration

Constants can be very useful if you need any value multiple times in your Java code. You can define a constant and use it anywhere, multiple times rather than writing duplicate code.

  • Always define a constant with all UPPERCASE.
  • Separate the words by an underscore( ‘_’ ).
public static final int VALUE_OF_A = 1;
public static final int VALUE_OF_B = 5;
public static final String WEBSITE_URL = "https://www.masterincoding.com";

1.6 Methods Declaration

Methods in Java should define what they are trying to do and what they are returning back.

Therefore, methods should be named as verbsĀ and always start with a lower case following with UPPERCASE.

void addUser(String value);
void deleteUser(String value);
int getCountOfUser();

I hope you’ve understood how you should design a Java class and what are the Java Naming Conventions you should follow.