Java Naming Convention

Naming convention are nothing but rules for writing names in java. Which may be class name, variables name or method name etc. For that Sun Microsystem given some rules.

This rules are recommended but not mandatory.

Naming convention for classes and interface

Every class you have to start with capital letter.

Example of class naming convention:


    //Naming Convention 1	
    class Employee
    {

    }
    
    //Naming Convention 2
    class Customer
    {
  
    }
    
    //Naming Convention 3
    class CourierTracking
    {
    
    }
    

Naming convention for variables

Every variables we have to start with small word. Single letter start with lower and second word must with capital.

Example of variables naming convention:


    gender, age, panNo, customerName, addressLine, phoneNumber;
    

Naming convention for methods

Method exactly same as a variable name. Every method we have to start with small word. Single letter start with lower and second word must with capital.

Example of methods naming convention:


    display()
    add()
    withdrow()
    getStatus()
    getId()
    personeDetails()
    areaAddress()
    

Naming convention for keywords

All words should be lower case.

Example of keywords naming convention:


    if, else, switch, case, for, etc....
    

Naming convention for packages

All words should be lower case with ( . ) symbol.

Example of packages naming convention:


    import java.util;
    import java.io;
    import java.lang;
    etc...
    

Naming convention for static constant variables

Static constant variables represent in upper letter.

Example of static constant variables naming convention:


    thread.MAX_PRIORITY
    thread.MIN_PRIORITY
    thread.NORM_PRIORITY
    

See the complete example of naming convention:


	class Student
	{
		int studentId= 123, age = 25, phoneNumber = 99157;
		String studentName = "Manzar";
		
		public void getDetails()
		{
			System.out.println("Student Id = "+studentId);
			System.out.println("Student Name = "+studentName);
			System.out.println("Student Age = "+age);
			System.out.println("Student Phone Number = "+phoneNumber);
		}
		 
	}

	class Record
	{
		public static void main(String arg[])
		{
			Student std = new Student();
			std.getDetails();
		}
	}
    

Output:


	Student Id = 123
	Student Name = Manzar
	Student Age = 25
	Student Phone Number = 99157

Post Your Comment