Java Naming Conventions

If you use java naming conventions while doing any program or developing any software. That is the best practice for a good programmer or developer that makes the readable code.

It is a standard that follows all developers when developing any software.

Why we use Naming Conventions in Java

We use naming conventions because:

  • It makes programs more understandable.
  • Easy to read.
  • It gives good code quality.
  • It also gives information about class, method or package.

CamelCase in java

Java follows the camel-case syntax for naming the class, interface, method, and variable.

If the name is in multiple words, the second word will start with uppercase letter always, for example, getId(), StudentsData(), ActionEvent, firstName, etc.

Rules that must be followed.

Class, method, variable and object name should not match with java keywords.

Let’s see all naming conventions in detail with examples.

Class

  • The name should begin with a capital letter in a single word.
  • If it is multiple words, the first letter of every word begins with capital letters.

Single word example:

public class Students
{  
//code
}  

Multiple words example:

public class EmployeeData
{  
//code
} 

Method

  • The first letter should begin with a lowercase letter in a single word.
  • If it is multiple words, the first letter of the first word should begin with lowercase letters and remain the first letter of every word should be a capital letter.

Single word example:

public void remove()
 {  
 //code
 }  

Multiple words example:

public void getName()
 {  
 //code
 }  

Variable

  • It should begin with a lowercase letter.
  • It should not begin with a digit.
  • Variables should not begin with the special characters like &, $, _.
  • Variable names should not contain white space.

Example:

public class Students
{  
//variable 
private int sid;
} 

Package

  • It should be a lowercase letter.
  • It should be separated by dots (.) if it is multiples words.
  • Package names should start with your domain name such as in.study4geeks.

Example:

package in.study4geeks;

Interface

  • Interfaces names should be adjectives.
  • It should start with the uppercase letter.

Example:

public interface CommonConstant  
{  
//code
} 
Do you know?
Java Naming Conventions This article is contribute by Study4Geeks If you want share your knowledge on Study4Geeks then go to login section.