The Wayback Machine - https://web.archive.org/web/20240621041700/https://www.geeksforgeeks.org/java-naming-conventions/
Open In App

Naming Conventions in Java

Last Updated : 20 Jun, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A programmer is always said to write clean codes, where naming has to be appropriate so that for any other programmer it acts as an easy way out to read the code. At a smaller level, this seems meaningless but think of the industrial level where it becomes necessary to write clean codes in order to save time for which there are certain rules been laid of which one of the factors is to name the keyword right which is termed as a naming convention in Java. 

For example when you are using a variable name depicting displacement then it should be named as “displace” or similar likewise not likely x, d which becomes complex as the code widens up and decreases the readability aperture. Consider the below illustrations to get a better understanding which later on we will be discussing in detail.

Illustrations: 

  • Class: If you are naming any class then it should be a noun and so should be named as per the goal to be achieved in the program such as Add2Numbers, ReverseString, and so on not likely A1, Programming, etc. It should be specific pointing what exactly is there inside without glancing at the body of the class.
  • Interface: If you are naming an interface, it should look like an adjective such as consider the existing ones: Runnable, Serializable, etc. Try to use ‘able’ at the end, yes it is said to try as there are no hard and fast bound rules as if we do consider an inbuilt interface such as ‘Remote’, it is not having ble at the end. Consider if you are supposed to create an interface to make read operation then it is suggested as per naming conventions in java to name a similar likely ‘Readable’ interface.
  • Methods: Now if we do look closer a method is supposed to do something that it does contains in its body henceforth it should be a verb.
  • Constants: As the name suggests it should look like as we read it looks like it is fixed for examples PI, MAX_INT, MIN_INT, etc as follows.

Naming Conventions in Java 

In java, it is good practice to name class, variables, and methods name as what they are actually supposed to do instead of naming them randomly. Below are some naming conventions of the java programming language. They must be followed while developing software in java for good maintenance and readability of code. Java uses CamelCase as a practice for writing names of methods, variables, classes, packages, and constants. 

Camel’s case in java programming consists of compound words or phrases such that each word or abbreviation begins with a capital letter or first word with a lowercase letter, rest all with capital. Here in simpler terms, it means if there are two 

Note: Do look out for these exceptions cases to camel casing in java as follows:

  • In package, everything is small even while we are combining two or more words in java
  • In constants, we do use everything as uppercase and only ‘_’ character is used even if we are combining two or more words in java.

Type 1: Classes and Interfaces

  • Class names should be nouns, in mixed cases with the first letter of each internal word capitalized. Interfaces names should also be capitalized just like class names.
  • Use whole words and must avoid acronyms and abbreviations.
Classes: class Student { }
class Integer {}
class Scanner {}
Interfaces : Runnable
Remote
Serializable

Type 2: Methods 

  • Methods should be verbs, in mixed case with the first letter lowercase and with the first letter of each internal word capitalized.
public static void main(String [] args)  {}

As the name suggests the method is supposed to be primarily method which indeed it is as main() method in java is the method from where the program begins its execution.

Type 3: Variables

Variable names should be short yet meaningful. 

Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.
  • Should be mnemonic i.e, designed to indicate to the casual observer the intent of its use.
  • One-character variable names should be avoided except for temporary variables.
  • Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters.
int[] marks;
double double answer,

As the name suggests one stands for marks while the other for an answer be it of any e do not mind.

Type 4: Constant variables

  • Should be all uppercase with words separated by underscores (“_”).
  • There are various constants used in predefined classes like Float, Long, String etc.
num = PI;

Type 5: Packages

  • The prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, like com, edu, gov, mil, net, org.
  • Subsequent components of the package name vary according to an organization’s own internal naming conventions.
java.util.Scanner ;
java.io.*;

As the name suggests in the first case we are trying to access the Scanner class from the java.util package and in other all classes(* standing for all) input-output classes making it so easy for another programmer to identify.

Note:

  • For class, interfaces and constants, the first letter has to be uppercase.
  • For method , variable, and package_name, the first letter has to be lowercase.

 


Previous Article
Next Article

Similar Reads

Validate Variable Names According to Naming Conventions in Java
Let's write a Java Program that checks whether a given variable name complies with the naming convention or not using Regular Expressions in Java. Example for Validating Name of Variable Using RegexInput: _myNameOutput: Correct Input: 2ndVariableOutput: Incorrect Regex to Check Variable Name is Valid according to Naming ConventionBelow is the imple
2 min read
java.rmi.Naming Class in Java
Java.rmi.Naming class contains a method to bind, unbind or rebind names with a remote object present at the remote registry. This class is also used to get the reference of the object present at remote registries or the list of name associated with this registry. Syntax: Class declaration public final class Naming extends Object Let us do discuss s
4 min read
Interface Naming Conflicts in Java
Interfaces in Java consist of abstract methods (which do not contain a body) and variables (which are public static final). Implementation of the methods of the interface is defined in classes that implement that interface. It helps Java to achieve abstraction. Naming Conflicts occur when a class implements two interfaces that have methods and vari
5 min read
Naming a thread and fetching name of current thread in Java
Thread can be referred to as a lightweight process. Thread uses fewer resources to create and exist in the process; thread shares process resources. The main thread of Java is the thread that is started when the program starts. now let us discuss the eccentric concept of with what ways we can name a thread. Methods: There are two ways by which we c
5 min read
Best Practices For Naming API Endpoints in a RESTful Architecture
Naming API endpoints is important in designing a clear and easy-to-use API. By using consistent and descriptive names, avoiding abbreviations, and following best practices for pluralization and lowercase letters, you can create an API that is easy to understand, use, and maintain. Naming API endpoints is an important part of designing a RESTful API
6 min read
Difference Between java.sql.Time, java.sql.Timestamp and java.sql.Date in Java
Across the software projects, we are using java.sql.Time, java.sql.Timestamp and java.sql.Date in many instances. Whenever the java application interacts with the database, we should use these instead of java.util.Date. The reason is JDBC i.e. java database connectivity uses these to identify SQL Date and Timestamp. Here let us see the differences
7 min read
How to Convert java.sql.Date to java.util.Date in Java?
If we have the Date object of the SQL package, then we can easily convert it into an util Date object. We need to pass the getTime() method while creating the util Date object. java.util.Date utilDate = new java.util.Date(sqlDate.getTime()); It will give us util Date object. getTime() method Syntax: public long getTime() Parameters: The function do
1 min read
Different Ways to Convert java.util.Date to java.time.LocalDate in Java
Prior to Java 8 for handling time and dates we had Date, Calendar, TimeStamp (java.util) but there were several performance issues as well as some methods and classes were deprecated, so Java 8 introduced some new concepts Date and Time APIs' (also known as Joda time APIs') present in java.time package. Java 7: Date, Calendar, TimeStamp present ins
3 min read
How to Convert java.util.Date to java.sql.Date in Java?
Date class is present in both java.util package and java.sql package. Though the name of the class is the same for both packages, their utilities are different. Date class of java.util package is required when data is required in a java application to do any computation or for other various things, while Date class of java.sql package is used whene
3 min read
Java AWT vs Java Swing vs Java FX
Java's UI frameworks include Java AWT, Java Swing, and JavaFX. This plays a very important role in creating the user experience of Java applications. These frameworks provide a range of tools and components for creating graphical user interfaces (GUIs) that are not only functional but also visually appealing. As a Java developer, selecting the righ
11 min read
Article Tags :
Practice Tags :