Java Coding Standards

Java Coding Standards

Whenever we are writing any Java code we have to follow some coding standards, These coding standards are followed in all types of java project development, for example- whenever we are writing any component, its name should reflect the purpose of that component (functionality).

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

class A{
public int a1(inta,intb)
{
return x+y;
}

Java Coding standards for Classes

Class names in Java should start with upper case characters and if they contain multiple words every inner word should start with an upper case character.

Example:-
Table
AccountNumber
UserName

Java Coding standards for Interfaces

Interface name should start with upper case characters, and if contains multiple words every inner word should start with an upper case character.

Example:-
Runnable
Comparable

Java Coding Standards for Methods

Methods name should start with the lowercase alphabet symbols and if it contains multiple words then every inner word should start with an uppercase character (camel case convention).

Example:-
drink();
eat();
walk();
run();
getName();
setSalary();  //Verb-Noun combination

Java Coding Standards for Variables

Variable’s name should start with lower case alphabet symbol, and if it contains multiple words then every inner word should start with an upper case character(camel case convention)

rollNo
age
mobileNumber
salary

Java Coding Standards for Constants

Constants name should contain only upper case character and if contains multiple words then these words are separated with an underscore symbol.

Example:-
MAX_VALUE
AVG_VALUE
MIN_VALUE
NORM_VALUE
  • Usually, we can declare constants with public static and final modifiers.

JavaBeans Coding Standards

A Javabean is a simple class with private properties with public Setter and Getter methods.

public class JavaBean {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

Syntax for set method(Setter)

  • It should be a public method.
  • The return type should be void
  • method name should be prefixed with set.
  • It should take some arguments that are it should not be any argument method.

Syntax for get method(Getter)

  • It should be a public method.
  • The return type should be void.
  • Method name should be prefixed with get.
  • it should not take any argument

For boolean properties get method name can be prefixed with either get or it’s recommended to use it.

private boolean empty;
public boolean get empty(){
return empty;
}
public boolean is empty()
{
return empty;
}

Java Coding Standards for Listeners

  • To register a listener compulsory the method name should be prefixed with add.
public void add MyActionListner(MyActionListner) //Valid
public void register MyActionListner(MyActionListner) //Invalid
public void add MyActionListner(ActionListner) //Invalid
  • To unregister a listener compulsory the method name should be prefixed with remove
public void remove MyActionListner(MyActionListener l) // Valid
public void unregister  MyActionListner(MyActionListener l) //Invalid
public void remove MyActionListner(ActionListener l) //Invalid

Similar Java Tutorials

3 thoughts on “Java Coding Standards”

Leave a Comment