Home >>Java Tutorial >Java Naming conventions

Java Naming conventions

Java Naming conventions

Java naming convention is basically a rule that is to be followed when the programmer decides what to name the identifiers like variable, class, package, method, constant and others. However, it is not mandatory to follow this rule hence, it is also known as a convention not rule. These conventions were first introduced by various Java communities some of the names are Netscape and Sun Microsystems.

The Java naming convention comes in handy when the programmers have to give names to all the packages, classes, interfaces, methods and fields of Java programming language. There is a chance that the Java language will generate confusion or erroneous code in case the programmer fails to follow these conventions.

Advantage of naming conventions in java

There are many advantages of the naming convention in Java such as the programmer can make their code easier to read for themselves as well as other programmers just by using standard Java naming conventions. The readability of any Java program plays a very crucial role hence making it important. It basically indicates that less time is being spent in order to figure out the working of the code.

Here are the key rules that are mandatory to be followed by every identifier are depicted below:

  • The name of any identifier must not contain any white spaces.
  • The name of any identifier should not start with the special characters like _ (underscore), & (ampersand), $ (dollar).

Let's understand the some other rules that are mandatory to be followed by the various identifiers

Class

The class's name should start with the uppercase letter and it must be a noun like Button, Colour, Thread, System, and many others. There should be appropriate use of words and acronyms should be avoided.

Here is an example of the class that will help you understand the implementation of these rules:

public class Student  
{  
//code snippet  
}  

Interface

The name of the interface should start with the uppercase letter that is common. The name must be an adjective like Action Listener, Runnable, Remote and any other. There should be appropriate use of words and acronyms should be avoided.

Here is an example of the interface that will help you understand the implementation of these rules:

interface Executable  
{  
//code snippet  
}  

Method

The name of the method in Java should start with the lowercase letter. The name must be a verb that should depict action like println(), main(), print().

In case the name consists of multiple words then it should start with a lowercase letter that is followed by an uppercase letter for instance, actionPerformed().

Here is an example of the methods that will help you understand the implementation of these rules:

class Student 
{  
//method  
void register()  
{  
//code snippet  
}
void login()  
{  
//code snippet  
}  

}  

Variable

The name of the variable should start with a lowercase letter for instance, id, name, etc. There should be no use of special characters such as _ (underscore), & (ampersand), $ (dollar), in the beginning.

In case, the name consists of multiple words then it should start with the lowercase letter that is followed by an uppercase letter for instance, firstName, lastName. The use of one-character variables like a, b, c should be avoided.

Here is an example of the variable that will help you understand the implementation of these rules:

class Student  
{  
//variable  
int roll;  
//code snippet  
}  

Package

The name of the packages in Java should be a lowercase letter for instance java, lang, etc. In case the name consists of multiple words then it should be separated by the help of dots (.) like java.util, java.lang, etc.

Here is an example of the package that will help you understand the implementation of these rules:

package mypackage; //package  
class Student  
{  
//code snippet  
}  

Constant

The name of the constant in Java should be in uppercase letters for instance, GREEN, YELLOW, etc. In case the name consists of multiple words then it should be separated by the help of an underscore (_) like MAX_PRIORITY. However, the name of the constant may consists of digits but it should not be used as the initial letter.

Here is an example of the constant that will help you understand the implementation of these rules:

class Student  
{  
//define constant  
 static final int DOLLAR_RATE = 74;  
//code snippet  
} 

CamelCase in java naming conventions

The camel-case syntax that is used for naming the interface, class, variable, and method is used by the Java language. In case the name is joined with two words then the second word should start with uppercase letter always for instance ActionListener, ActionEvent, actionPerformed(), firstName, etc.


No Sidebar ads