Open In App

Naming Conventions in Java

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: 

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

Classes: class Student { }
         class Integer {}
         class Scanner {}
Interfaces : Runnable
             Remote
             Serializable 

Type 2: Methods 

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.
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

num = PI;

Type 5: Packages

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 and interfaces, the first letter has to be uppercase.
  • For method , variable, package_name, and constants, the first letter has to be lowercase.

 

Article Tags :