Whenever we are writing java 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.
Example:
Usually class names are nouns, should starts with upper case character and if contains multiple words every inner word should starts with upper case character.
Example:
Coding standard for interfaces:
Usually interface names are adjectives, should starts with upper case character and if contains multiple words every inner word should starts with upper case character.
Example:
Usually method names are either verbs or verb-noun combinations, should starts with lower case character and if contains multiple words every inner word should starts with upper case character.(camel case convention)
Example:
Coding standard for variable:
Usually variable names are nouns, should starts with lower case character and if contains multiple words every inner word should starts with upper case character.(camel case convention)
Example:
Usually constant names are nouns, should contain only upper case characters and if it contains multiple words, then these words are separated with underscore(_) symbol.
Example:
Note: Usually we can declare constants with public static and final modifiers.
Coding standard for JavaBean:
A Java Bean is a simple java class which contains some private variables along with public setter and getter methods for those variables.
Example:
Syntax for setter method:
Syntax for getter method:
Note: for boolean property getter method name can be prefixed with either “get” or “is”, but recommended to use “is”
Example:
Coding standard for Listeners:
case 1:
To register a listener:
method name should be prefixed with “add”
method should be public
Example:
case 2:
To de-register a listener:
method name should be prefixed with “romove”
method should be public
Example:
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.
Example:
//Not Recommended class A { public int m1(int x, int y) { return x + y; } }
//RecommendedCoding standard for class:
package com.learning.java public class Calculator { public static int sum(int firstNumber, int secondNumber) { return firstNumber + secondNumber; } }
Usually class names are nouns, should starts with upper case character and if contains multiple words every inner word should starts with upper case character.
Example:
String
StringBuilder
Account
Student
Coding standard for interfaces:
Usually interface names are adjectives, should starts with upper case character and if contains multiple words every inner word should starts with upper case character.
Example:
RunnableCoding standard for methods:
Serializable
Comparable
Usually method names are either verbs or verb-noun combinations, should starts with lower case character and if contains multiple words every inner word should starts with upper case character.(camel case convention)
Example:
print()
run()
start()
setName()
getSalary()
Coding standard for variable:
Usually variable names are nouns, should starts with lower case character and if contains multiple words every inner word should starts with upper case character.(camel case convention)
Example:
nameCoding standard for constant:
age
MobileNumber
Usually constant names are nouns, should contain only upper case characters and if it contains multiple words, then these words are separated with underscore(_) symbol.
Example:
MAX_VALUE
PI
MAX_PRIORITY
MIN_PRIORITY
Note: Usually we can declare constants with public static and final modifiers.
Coding standard for JavaBean:
A Java Bean is a simple java class which contains some private variables along with public setter and getter methods for those variables.
Example:
public class Student{class name ends with Bean is not official convention. i.e “StudentBean” naming convention is not recommended.
private String name;
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
}
Syntax for setter method:
- It should be public method.
- Return type should be void.
- Method name should be prefixed with set
- method should contain some arguments
Syntax for getter method:
- It should be public method.
- Return type should not be void.
- Method name should be prefixed with get
- method should not contain any argument
Note: for boolean property getter method name can be prefixed with either “get” or “is”, but recommended to use “is”
Example:
private boolean empty;
public boolean isEmpty(){
return empty;
}
public boolean getEmpty(){
return empty;
}
Coding standard for Listeners:
case 1:
To register a listener:
method name should be prefixed with “add”
method should be public
Example:
public void addMyActionListener(myActionListener l) //valid
public void registerMyActionListener(myActionListener l) //Invalid
public void addMyActionListener(ActionListener l) //Invalid
case 2:
To de-register a listener:
method name should be prefixed with “romove”
method should be public
Example:
public void removeMyActionListener(myActionListener l) //valid
public void unRegisterMyActionListener(myActionListener l) //Invalid
public void addMyActionListener(ActionListener l) //Invalid
public void deleteMyActionListener(myActionListener l) //Invalid