Java Coding Standard

Java coding standard Whenever we are writing Java code is highly recommended to follow

Java coding standard

.
Whenever we are writing any component its name should reflect the purpose of the component(functionality).
The main advantage of this approach is readability and maintainability of the code will be improved.

Example partially correct

class A
{
	public int m1(int x,int y)
	{
		return x+y;
	}
}

Example pure correct

package com.javaexpert;
public class calculator
{
	public int add(int number1,int number2)
	{
		return number1+number2;
	}
}

Coding standard for classes

usually class names are nouns should starts with uppercase character undefeed multiple word should starts with uppercase character.

Example : –

  1. String
  2. StringBuffer
  3. Account
  4. Dog

Coding standard for interface

usually interface names are adjective should start with uppercase character and if it contants multiple words every inner word starts with uppercase character.

Example : –

  1. Runable
  2. Serialzable
  3. Compareble
  4. etc…

Coding standard for method

Usually method name are ether verbs are verb-Noun combination. should starts with lowercase alphabet symbol and if it contants multiple words then every inner word. should start uppercase character(Camel case convention).

Example : –

  1. print()
  2. sleep()
  3. run()
  4. eat()
  5. getName()
  6. getSalary()

Coding standard for variable

Usually variable names are nouns should start with lowercase alphabet symbol if it containts multiple words then every inner word should start with uppercase character (Camel case convention).

Example : –

  1. name
  2. age
  3. mobileNumber

Coding standard for constant

Usually content names are nouns should conten only uppercase character if it contain multiple words then these words are seperated with underscore symbol.

Example : –

  1. MAX_VALUE
  2. MAX_PRIORITY
  3. NORM_PRIORITY
  4. MIN_PRIORITY

Note : –

Usually we can declare contants with public static and final modifires

Java Bean coding standard : –

A JavaBeans is a simple Java class with private property’s and public getter and setter methods.

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

Syntax for setter method

  1. it should be public method.
  2. the return type should be void
  3. method name should be prefixed with set
  4. it should take some argument . that is it should not be no argument method.

Syntax for getter method

  1. it should be public method.
  2. Thr return type should not be void
  3. method name should prefix with get
  4. it should not take any argument

Note : –

For boolean property getter method name can be prefix with ether get are is but recommanded to use is

This is valid

class Demo 
{
	private boolean Empty;
	public boolean getEmpty()
	{
		return Empty;
	}
}
This is also valid

class Demo 
{
	private boolean Empty;
	public boolean isEmpty()
	{
		return Empty;
	}
}

Coding standard for Listeners

Register a Listener : –

Method name should be prefix with add .

Valid Example : –

public void addMyActionListener(MyActionListener e)

InValid Example : –

public void registerMyActionListener(MyActionListener e)

unregister a Listener : –

Method name should be prefix with remove .

Valid Example : –

public void removeMyActionListener(MyActionListener e)

InValid Example : –

public void unregisterMyActionListener(MyActionListener e)

public void removeMyActionListener(ActionListener e)
< /p>

Java coding standard

Leave a Reply

Your email address will not be published. Required fields are marked *