Java Programming - Best Practices

For any programmer it is very important
to follow the coding standard,best practices to write better code.Following the best practice helps in better design, performance, maintainability, and optimization.In this post, we are going to share best practices for any Java developer.
  • Naming Convention
  1. The class name must be a Noun and it must start with capital letter.It follows UpperCamelCase.
  2. Method name must be a verb follow lowerCamelCase
  3. Variables must be a noun and follow lowerCamelCase

  • Don’t declare the variable inside the loops - If a variable is declared inside the loop every time loop executes this variable get declares and occupy space.


  • Add the open curly brace immediately after the method or constructor.

  • Use enhanced for loop to iterate collections and arrays - Enhanced for loop internally use an iterator.

  • Add no arguments constructor for all the class- If we don't add any argument constructor JAVA add a default constructor on its own but JAVA adds no argument only when there is no other constructor is added manually.A framework such Spring and Hibernate create an instance using reflection which needs no argument constructor.If it doesn't found any no-argument constructor it will throw instantiation exception.


  • Always Release the resources 

  1. Use finally blocks to close streams and connections.Finally block always executed and it important to close and connections as it may cause memory issue.
  2. Before closing the stream always check if it is not null.

  • Exceptions

  1. If the exceptions are propagated handle it to avoid stack trace .
  2. Do not throw the exception from the main method.
  3. Do not catch generic exception (supertype) catch.Always catch the most specific exception(subtype).For example- Do not catch FileNotFoundException ins Exception block.

  • Check the instance using instanceOf then type caste-To avoid class cast exception.
  • Single quote and Double quote-Use single quote and double quote carefully as single quote denote character while double quote is for String.

Hope you learn some new java best practices.We will add more points in this list ,so bookmark it.

You may be interested in Java- Basic Tutorial

Give you suggestion in comments.If you like the post Share with your friends on social network.

6 Different ways to iterate through HashMap


Java Programming - Best Practices Java Programming - Best Practices Reviewed by JavaInstance on 11:11:00 AM Rating: 5

No comments:

Powered by Blogger.