Java Naming Convention

    Naming Convention

    The naming convention specifies the rules for naming the identifiers like class, object, methods, variables, etc., within any program . These conventions are forced to use but are suggested to keep the rules in mind.

    Every name that is given to the identifiers is according to the Java naming convention. It is better to follow the convention to avoid errors.

    Benefits of Using the Naming Convention

    It makes coding reading much easier for other coders and programmers. It allows the coders to take less time to understand the code. Below are the important points to note-

    • There should be no white space for any identifier name.
    • You cannot start the name with any special character.

    Class

    • The class name should start with a capital letter.
    • It should indicate any noun.
    • Try to use appropriate words instead of using acronyms.

    Example

    public class Student
    {
        //code
    }

    Interface

    • Like class, it should also be started with an uppercase letter.
    • It should be an adjective
    • Do not use the acronym.

    Example

    interface Run  
    {  
      //code
    }  

    Method

    • You should use the lowercase for the first letter.
    • It should be a verb indicating the behavior.
    • If you are using multiple words for the method name, then the first letter should be lowercase, and the first letter of the second word should be uppercase.

    Example

    class Employee  
    {  
       //method  
       void drawHere()  
        {  
           //code
        }  
    }  

    Variable

    • The variable name should start with the lowercase.
    • Do not start the variable with the special character.
    • You must avoid using single-character variable names.
    • If you are using multiple words for the method name, then the first letter should be lowercase, and the first letter of the second word should be uppercase.

    Example

    class Employee  
    {   
        int id; 
        int varName; 
       //code 
    }  

    Package

    • It should be in the lowercase.
    • If you are using a package name that is having multiple words, then it should be separated by (.).

    Example

    package java.utilt; 
    
    class Employee  
      {  
       //code
      } 

    Constant

    • You should use uppercase for defining the constant.
    • If you are using a constant name with multiple words, it should be separated by (_).
    • You can use digits in the constant name but not in the first place.

    Example

    class Students 
    {  
       static int MIN_AGE = 18;  
       //code
    }