What is Naming Convention in Java.?

Naming Convention are nothing but rules for writing names in java. Which may be class name, variables name and 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 word in lower case.

Example of keywords Naming Convention:


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

Naming Convention for Packages

All are in 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:


	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

Advertisement here

Share this Page.