Recent Posts

Sunday, 29 May 2016

Java Coding Standards


* Whenever we are writing the code it is highly recommended to follow coding standards.

* Whenever we are writing any component it's name should reflect the purpose of that component (Functionality)

* The main advantage of this approach is readability and maintainability of the code will be improved.

Coding Standards For classes
* Usually class names are nouns. Should starts with upper case letter and if it contains multiple words every inner word starts with upper case letter.
E.g
Employee
Student
Account
String
StringBuffer
Coding Standards For Interfaces
* Usually interface names are adjectives. Should starts with upper case letter and if it contains multiple words every inner word starts with upper case letter.
E.g
Runnable
Clonable
Movable
Serializable
Coding Standards For Methods
* Usually method names are either verbs or verb noun combination. Should starts with lower case letter and if it contains multiple words every inner word starts with upper case letter.
E.g
get()
run()
sleep()
wait()
getName()
setNumber()
Coding Standards For Variables
* Usually variable names are nouns. Should starts with lower case letter and if it contains multiple words every inner word starts with upper case letter.
E.g
name
rollno
mobileNumber
salary
Coding Standards For Constants
* Usually constants are nouns. Should contains only upper case letters and if it contains multiple words these words are seperated with "_" symbol.
E.g
MAX_VALUE
MIN_VALUE
MAX_PRIORITY
MIN_PRIORITY
Java Bean Coding Standards
* A Java bean is a simple Java class with private properties (variables) and public getter and setter methods.
E.g
public class Employee {
   private int empId;
   private String empName;
   private String empAddress;
   private String empDesignation;
   private String empEmail;
   public int getEmpId() {
      return empId;
   }
   public void setEmpId(int empId) {
      this.empId = empId;
   }

   public String getEmpName() {
      return empName;
   }
   public void setEmpName(String empName) {
      this.empName = empName;
   }

   public String getEmpAddress() {
      return empAddress;
   }
   public void setEmpAddress(String empAddress) {
      this.empAddress = empAddress;
   }

   public String getEmpDesignation() {
      return empDesignation;
   }
   public void setEmpDesignation(String empDesignation) {
      this.empDesignation = empDesignation;
   }

   public String getEmpEmail() {
      return empEmail;
   }
   public void setEmpEmail(String empEmail) {
      this.empEmail = empEmail;
   }
}
Syntax for setter method
     The method name should be prefix with "set". Compulsory the method should take some argument and return type should be void.  It should be public method.

Syntax for getter method
     The method name should be prefix with "get". Compulsory the method should be no argument and return type should not be void.  It should be public method.
Note
     For boolean property the getter method can be starts with either "get" or "is" but recommend to use "is". 

Coding Standards For Listeners
Case 1 : Register a listener 
* Method name should be start with "add"

* After add whatever we are taking the argument should be same
E.g
addMyActionListener(MyActionListener mal)
Case 2 : Unregister a listener 
* Method name should be start with "remove"

* After add whatever we are taking the argument should be same
E.g
removeMyActionListener(MyActionListener mal)

Next Tutorial   Operators and Assignments

Previous Tutorial  Command Line Arguments

No comments:

Post a Comment