Home » Java Tutorial » Java Coding Conventions

Java Coding Conventions

Welcome to the second tutorial of Java tutorial series, In this tutorial, we are going to see the java coding conventions that make coding more fun and also make each line of code more readable. Following these coding, conventions not only make a readable code but also help other fellow programmers to walk through code with ease of better understanding of code. but if you want to read about Java then move to Introduction to Java i.e first tutorial of this series.

Wikipedia defines Coding convention as

“Coding conventions are a set of guidelines for a specific programming language that recommend programming style, practices and methods for each aspect of a piece program written in this language.”

So we can see, coding conventions are a bunch of guidelines but are it mandatory. Then my answer is no but remembers coding conventions always make life easier. So let’s see the reason why do we need to learn Java Coding Conventions-

  • Code Readability
  • Ease to grasp the code
  • Clean coding and better packaging of the product.

But before moving ahead let’s remember few much-talked components of Java these are

  • Class
  • Interface
  • Object
  • Variable
  • Method
  • Constructor
  • Comments

Wait for a minute, Think once and let me know. Are you able to differentiate one component of your code to another? What if you are just going through your existing codebase. But nowhere you see any standard has been followed where method looks like a Class and Class looks like a method. In such a scenario you should remember the Coding Conventions. This is not because you are going to find peace of mind but this is for the programmer who is going to take the ownership after you.

So let’s jump into the list of Java Coding Conventions that every programmer in java ought to follow.

1- File extension of Java file :
Java file has extension .java and byte-code will have .class and to use all these classes we can have java archive file with name .jar.

2- Comment in Java 

  1. Single line comment: For single-line comment, one can use “//” and whatever we write after this // would be ignored by compiler For Example:

But for multi-line comment, we can use /* followed by your comment */


3- Methods: Method Name should start with lower case letter and if the method contains multiple words for better readability then each inner word’s first letter should be in caps or in Upper Case. Along with this, we should try to keep the name of method as a verb.


Format of the writing method

4- Classes: Each class should start with the first letter in Upper Case and again like method, if the class name is made up of multiple words then each inner word’s first letter should be in upper case.


Format of the class declaration:

5- Constructors: It will always be same as for Class name. A constructor does not have any return type that’s why we never write anything like void/any primitive data type as return type.

6- Variables:  It should start with lower case and like method, if a variable is made up of more than one words and the first letter of each inner word would be in Upper Case. We should try to keep a short name for variables and should avoid multiple words in variable name in code convention, both variable and methods seem similar only parentheses bring the difference between a variable and method.



8- Constants: Constant should be in complete Upper Case ex: SUNDAY, MONDAY
9- Structure of a Java Class

a- Beginning comment

b- Package name and import statement

c- Class or interface declaration

d- Inside class order of variable

i- Static variable should come first

ii- public variable

iii- protected variables

iv- private

e- After variable declaration, Constructor should be declared

f- Inside class, the method should be in functional order there is no ordering of method on the basis of scope or access modifier.

ex: Java generic class

10- Each line of code should terminate with “;”

11- For Readability purpose, try to keep each line at max 80 characters because many terminals do not support more character length of 80.

12- For better readability, we should wrap the lines of code if going beyond 80 characters

Some thumb rules for wrapping lines

  • Break after a comma.


  • Break before an operator.


13- Single declaration in a line increase better readability and also support comment associated with declaration.


14- Blank line and white space: Blank line always increase the readability of code and also make a more visible section of code as well. Following are some convention that should be followed in java

1- Two blank line

a- Between a class definition and interface definition

b- Between two functional sections in a single class file

2- Single blank line

a-  Before each single line comment

b- Between two methods

c- Between variables, methods and a single line comments

d- Between logical sections of code inside the method for better readability.

3- Single-space should be present between each binary operator except. , and also should be avoided with unary operators.

4- Single-space should be present between parentheses with Java Keywords.
ex: while () and one space should also be present in the method name and parentheses.

So now we have completed a list of the coding convention of Java. So enjoy writing your Selenium WebDriver using Java.

Wish you all a very happy coding!!!

Here is the cheat sheet in the form of mind-map

Java Code Convention

1 thought on “Java Coding Conventions”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top
%d bloggers like this: