Sunday, 13 December 2020

Coding standards in Java

 Coding standards in Java :

Java programming language has defined some coding standards for class, variables, methods, interface which has to be followed.

Standards for methods in java:

  • In java programming language coding standards for method is usually name of method  should either be verb or noun combination and it has to start with lower letter.
  • If method name has multiple word than every first character of inner word must start with uppercase.


    Ex: print(), println(), sleep(), setAttribute(), getAttribute().

Standards for Variables in java:

  • The variable names in java programming language usually should be noun and start with lowercase letter.
  • If variable name has multiple word than every first character of inner word must start with uppercase.


    Ex: name, age, email, mobileNumber, studentId.

For Classes:

  • Coding standard of class in java programming language is name of class  should be noun and it has to star with uppercase letter.
  • If class name has multiple word than every word must start with uppercase.


    Ex: String, System, StringBuffer, StringBuilder, Thread

For Interface:

  • Unlike class interface in java language usually name of interface must be adjective and interface name must start with uppercase letter.
  • If interface name has multiple word than every first character of word must start with uppercase.


Eg:  Serializable, Runnable



Constants:

  • These names should be a noun and these should contain only uppercase.
  • If the name of constant consists of multiple word than thye should be separated with ( _ ) underscore.

    
    Ex: MAX_PRIORITY, MIN_PRIORITY.



Getter Methods:


  • Getter should be public method.
  • Method name should be prefixed with get.
  • Getter should not take any parameter.

   syntax:   getXXXX( )
    
    Ex.  getInt( ), getString( ), getDate( ).

Setter Methods:


  • Setter should be public method and return Type of setters should be void.
  • Method names of setters should be prefixed using set.
  • They take some argument.

    

    syntax:   setXXXX( )


    Ex.  setInt( ), setString( ), setAttribute( ).

This is a java program for getters and setters:

public class EmployeeBean{

    private int empId;
    private String name;

    public void setId(int id){
        this.empId=id;
    }

    public int getId(){
        return empId;
    }
    public void setName(String name){
        this.name=name;
    }

    public String getName(){
        return name;
    }

}

No comments:

Post a Comment