I observed that many programmers do not create separate functions.
They write the whole program in the main function.
Doesn't this reduce its modularity?
eg:

class abc
{
public static void main(String args[])
{
int a=10;
System.out.println(++a);
}
}

it could also be written as:-

class abc
{
private int a=10;

public void calc()
{
	System.out.println(++a);
}

public static void main(String args[])
{
	abc a1=new abc();
	a1.calc();
}

}

I do not understand the reason to why this is done.
the eg given is of a small program but if it was a big one then according to me there wold be a problem in handling the first program.
Please explain me.

Recommended Answers

All 4 Replies

If they are writing everything (or even alot) in main, then they are not using Java properly. They are attempting to perform procedural programming using an Object-Oriented language. Ignore them.

commented: Funny, direct and correct! =) +4

I observed that many programmers do not create separate functions.
They write the whole program in the main function.

What do you mean by saying: "many programmers". Because this is not the right way to do it. In any language. You must write small functions that are called in the main.
Most programmers that write large pieces of code in an efficient manner, write things in separate functions that are organized in different classes depending on their functionalities.
These functions are not necessarily used only once in main. They could be used by other different function more than once. So if you want to make a change, you only change the function. If you had most of the things in main then it would be very difficult to change or correct something.
Also you wouldn't have to repeat the same code all over again when you can only call one function

So the second approach is more preferable even in small programs

if they're to lazy to create a class, and think it's faster just to write in main, let them, and just wait and see what happens if they need to re-use some code.
not to mention that if their bosses are not smart or involved enough to see that their developpers aren't capable of writing some decent OO code, that's their problem

commented: I agree, but remember that main itself required a class! #_# +4

Thanks for the help.
It cleared my doubt.
Truly appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.