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

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

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

Running your own one person company

Recently there was a post on PuneTech on mom's re-entering the IT work force after a break. Two of the biggest concerns mentioned were : Coping with vast advances (changes) in the IT landscape Balancing work and family responsibilities Since I have been running a one person company for a good amount of time, I suggested that as an option. In this post I will discuss various aspects of running a one person company. Advantages: You have full control of your time. You can choose to spend as much or as little time as you would like. There is also a good chance that you will be able to decide when you want to spend that time. You get to work on something that you enjoy doing. Tremendous work satisfaction. You have the option of working from home. Disadvantages: It can take a little while for the work to get set, so you may not be able to see revenues for some time. It takes a huge amount of discipline to work without a boss, and without deadlines. You will not get the benefits (insuranc