Skip to main content

Coding conventions - class and method structure in Java

It is very important to follow a consistent coding structure in classes and methods. Following a consistent structure will increase the readability of your programs. Very often companies have their own set of guidelines for namng and coding conventions. If the company you work for has it's own standards then you should follow those. All team members should ideally adhere to the same standards to make the code more readable. If everyone follows their own individual standards, the developers who have to maintain the code will find it very difficult to understand the code, as they navigate from one class to another. It is like driving on a street where everyone drives on the left till you reach a stop light, after which everyone is driving on the right.  Most people will manage, but it will be difficult and slow.

I have shown a simple class below with all the elements that a class usually comprises of. Click on the play button to hear an explanation of the class structure.

01 public class ClassStructure {
02   //we first declare constants
03   public static final int MAX_VALUE = 10;
05   //then public attributes
06   public String someString;
07   public int someValue;
09   //then private attributes
10   private String name;
11   private long salary;
13   //then constructors
14   public ClassStructure(String name) {
15 = name;
16   }
18   //then public methods
19   public void parseClass() {
20     parseElement();
21   }
23   protected void updateSalary() {
24     //do something
25   }
27   //then private methods
28   private void parseElement() {
29     //do something
30   }
31 }

Notes: This text was originally posted on my earlier blog at
Here are the comments from the original post

DATE: 01/16/2007 05:09:54 AM
Hi Parag,
that was a pretty good introduction.Only thing i had to add is that i think we should be declaring unmodifiable attributes as final.For example in the sample attribute "name" could be declared as final as it is not getting updated after being initialized in the constructor.

DATE: 01/17/2007 05:34:29 AM
Hey B.M.
That is a good suggestion.

AUTHOR: Supriya Shinde
DATE: 02/02/2007 02:16:49 PM
Hi Parag Sir,
Being a beginner readability forms an imporatant part for understanding the codes. But, i would also like to know if for IT audits there are some particular or standard conventions that are used for the auditing purpose. That is, on what basis are the codes exactly audited. Are these conventions important for auditing purpose or only for readability convinience?

AUTHOR: Mitesh Desai, SCIT
DATE: 02/02/2007 02:20:06 PM
Hello sir,

I would like to add upon your coding convention that we can also have conventions for public and private variables and objects. like we can have following variable names:

if x is an object of a class: - o_x
if x is a private variable: - p_x
if x is a public variable: - m_x

we can have similar conventions for more efficient and readable codes.

DATE: 02/02/2007 05:32:34 PM
Supriya, these conventions are used purely for readability. Audits are done on various factors, and code conventions may be one of them... however the primary reason is to make the program more readable for those who will maintain it.


DATE: 02/02/2007 05:37:00 PM
Mitesh, using special variable names to add extra meaning to the variable should be avoided. In fact many people prefer not to do that because if you change the variable from let's say protected to public, you must change the name also. This can break a lot of programs that depend on that name. We should avoid adding metadata to variable names.



Popular posts from this blog

Five Reasons Why Your Product Needs an Awesome User Guide

Photo Credit: Peter Merholz ( Creative Commons 2.0 SA License ) A user guide is essentially a book-length document containing instructions for installing, using or troubleshooting a hardware or software product. A user guide can be very brief - for example, only 10 or 20 pages or it can be a full-length book of 200 pages or more. -- As engineers, we give a lot of importance to product design, architecture, code quality, and UX. However, when it comes to the user manual, we often only manage to pay lip service. This is not good. A usable manual is as important as usable software because it is the first line of help for the user and the first line of customer service for the organization. Any organization that prides itself on great customer service must have an awesome user manual for the product. In the spirit of listicles - here are at least five reasons why you should have an awesome user manual! Enhance User Satisfaction In my fourteen years as a

Inheritance vs. composition depending on how much is same and how much differs

I am reading the excellent Django book right now. In the 4th chapter on Django templates , there is an example of includes and inheritance in Django templates. Without going into details about Django templates, the include is very similar to composition where we can include the text of another template for evaluation. Inheritance in Django templates works in a way similar to object inheritance. Django templates can specify certain blocks which can be redefined in subtemplates. The subtemplates use the rest of the parent template as is. Now we have all learned that inheritance is used when we have a is-a relationship between classes, and composition is used when we have a contains-a relationship. This is absolutely right, but while reading about Django templates, I just realized another pattern in these relationships. This is really simple and perhaps many of you may have already have had this insight... We use inheritance when we want to allow reuse of the bulk of one object in other

Planning a User Guide - Part 3/5 - Co-ordinate the Team

Photo by  Helloquence  on  Unsplash This is the third post in a series of five posts on how to plan a user guide. In the first post , I wrote about how to conduct an audience analysis and the second post discussed how to define the overall scope of the manual. Once the overall scope of the user guide is defined, the next step is to coordinate the team that will work on creating the manual. A typical team will consist of the following roles. Many of these roles will be fulfilled by freelancers since they are one-off or intermittent work engagements. At the end of the article, I have provided a list of websites where you can find good freelancers. Creative Artist You'll need to work with a creative artist to design the cover page and any other images for the user guide. Most small to mid-sized companies don't have a dedicated creative artist on their rolls. But that's not a problem. There are several freelancing websites where you can work with great creative ar