Naming Conventions in Java
Last Updated :
20 Jun, 2024
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.
Similar Reads
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 i
5 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 fin
4 min read
Java | Renaming a file
In Java we can rename a file using renameTo(newName) method that belongs to the File class. Declaration: Following is the declaration for java.io.File.renameTo(File dest) method: public boolean renameTo(File dest) Parameters: dest - The new abstract pathname for the existing abstract pathname. Excep
1 min read
String Class in Java
A string is a sequence of characters. In Java, objects of the String class are immutable, which means they cannot be changed once created. In this article, we are going to learn about the String class in Java.Example of String Class in Java:Java// Java Program to Create a String import java.io.*; cl
7 min read
Encapsulation in Java
In Java, encapsulation is one of the coret concept of Object Oriented Programming (OOP) in which we bind the data members and methods into a single unit. Encapsulation is used to hide the implementation part and show the functionality for better readability and usability. The following are important
10 min read
Coding Guidelines in Java
Java is one of the most popular and widely used programming languages and platforms. A platform is an environment that helps to develop and run programs written in any programming language. Java is fast, reliable, and secure. From desktop to web applications, scientific supercomputers to gaming cons
7 min read
AbstractCollection toString() Method in Java
In Java, the toString() method is defined in the Object class and is inherited by all the Java classes. It is used to return a string representation of an object. The AbstractCollection class is a part of the Java Collections Framework, which overrides this method to provide a string representation
1 min read
Java Programming Course : A Complete Guide
Hey tech Geeks! Welcome back! Thinking to begin learning Java from scratch to pro level! No worries, get ready to complete your learning journey as GeeksforGeeks 'Master Java Programming Course' is here to be your learning partner. Java as being the most object-oriented & network- centric langua
6 min read
Compare two Strings in Java
String in Java are immutable sequences of characters. Comparing strings is the most common task in different scenarios such as input validation or searching algorithms. In this article, we will learn multiple ways to compare two strings in Java with simple examples.Example:To compare two strings in
4 min read
Java - symbolic constants
In Java, a symbolic constant is a named constant value defined once and used throughout a program. Symbolic constants are declared using the final keyword. Which indicates that the value cannot be changed once it is initialized.The naming convention for symbolic constants is to use all capital lette
2 min read