JavaIntellectualTricks

Java Coding Standards

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:
//Not Recommended
class A {
 public int m1(int x, int y) {
  return x + y;
 }
}
//Recommended
package com.learning.java public class Calculator { public static int sum(int firstNumber, int secondNumber) { return firstNumber + secondNumber; } }
Coding standard for class:
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:
Runnable
Serializable
Comparable

Coding standard for methods:
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:
name
age
MobileNumber
Coding standard for constant:
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{
private String name;
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
}
class name ends with Bean is not official convention. i.e  “StudentBean” naming convention is not recommended.

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

1 comment:

  1. This blog post on Java Coding Standards is a great resource for any Java developer looking to improve their code quality and consistency. The author does a fantastic job breaking down the importance of coding standards and offers practical tips and guidelines to follow. I appreciate the emphasis on readability, maintainability, and best practices when writing Java code. Overall, a very informative and well-written article that I will definitely be referencing in my own coding projects. Thank you for sharing this valuable information!
    Java classes in Pune

    ReplyDelete