Java naming conventions for interface, class, package, variable, reference variable to object, method, constant - using code comments in java


Java provides naming convention to name following >
  • 1) interface,
  • 2) class,

  • 3) package,
  • 4) variable or
    reference variable to object
  • 5) method,

  • 6) constant

java naming conventions uses Camel case >
Camel case means when more than two words are used to form a single word than first letter of all words except first word must be in upperCase/ capitalLetter.


Example >
In Camel case >
my name is ankit
myNameIsAnkit




Is it mandatory for developers to use Java naming conventions?
No, developers are free to follow or not to follow these naming conventions, developers may ignore them completely.

So, the question comes when we are free to ignore these conventions then why to use these conventions?
>Conventions make code more readable,
>Conventions make code more understandable.
>It becomes easy for developers to understand the other developers code.



Let’s discuss Naming Convention for following in detail >

1) package -
package name always begin with a smallCase/non-capital letter.
Example - java.lang

If package name is formed from multiple words than first letter of all words except first word must be in upperCase

Creating own package >
package myPackage;


2) interface -
interface name always begin with a upperCase/capital letter.
Example - java.lang.Serializable

If interface name is formed from multiple words than first letter of each word must be in uppercase.

Creating own interface >
interface MyInterface{
   //code....
}




3) class -
class name always begin with a upperCase/capital letter.
Example - java.lang.Exception

If class name is formed from multiple words than first letter of each word must be in uppercase.
Example - java.lang.ClassCastException

Creating own class >
class MyClass{
   //code....
}



4) variable -
variable name always begin with a smallCase.

If variable name is formed from multiple words than first letter of all words except first word must be in upperCase

Creating own variable >
int myId;


variable or reference variable to object
name of reference variable always begin with a smallCase.

If reference variable is formed from multiple words than first letter of all words except first word must be in upperCase

Using reference variable >
String obj = new String("abc");
obj is referring to new String("abc")


5) method -
method name always begin with a smallCase.

If method name is formed from multiple words than first letter of all words except first word must be in upperCase
Example - isEmpty() method of ArrayList.


Creating own method >
void myMethod(){
}


6) constant -
constants consist of upperCase letters only.
Example - isEmpty() method of ArrayList.


If constant name is formed from multiple words than words must be separated from underscore i.e ( _ )

Example - public final static int MAX_PRIORITY = 10; constant is found in java.lang.Thread class.


How to create constant in java?
constant can be created by using static final keywords in java. interface can only declare constants.

Creating own constant >
final static int MY_CONSTANT = 1;



Let’s create program to show all the above naming convention in one program >
package myPackage;
interface MyInterface{ //interface
}
class MyClass{ //class
   int myId; //variable
  
   String obj=new String("abc"); //variable / reference variable to object
   void myMethod(){ //method
   }
  
   public final static int MY_CONSTANT=1; //constant
}





Using code comments in java>

Java allows >
  • Single line comment &
   //Single line comment

  • Multi line comments
   /*
   * Multi line comment..
   */


All the code used inside comments is ignored during compilation.
Using comments makes program more readable and understandable.


Real time practice>
  • Method level comments - In front or above variable to mention purpose of using variable.
  • Method level comments - Method level comments written just above method to mention purpose of creating method. We might write logic there so that other developers could understand the purpose of method easily.
  • Class level comments - Class level comments written just above class to mention purpose of creating class.

Java docs always provide variable, method and class level comments.

>It becomes easy for developers to understand the other developers code.

Program to demonstrate variable, method and Class level comments
/*
* Class is used to represent Employee
*/
class Employee {
  
   //employee id
   int empId;
  
   /*
   * Method prints the employee id
   */
   void printId(){
          System.out.println(empId);
   }
}

/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */

RELATED LINKS>

Source file naming rule in java - When source file consists of public class & non-public classes OR only non-public classes


Labels: Core Java
eEdit
Must read for you :