What should and shouldn't I include in "main" method in java? | SoloLearn: Learn to code for FREE!

+1

What should and shouldn't I include in "main" method in java?

I thought, that all my program in java should be included in "main" method. But, as I see know in java tutorial, it is not necessary. The declarations of other methods, for examples, are carried out of "main" method. Is there some rule upon it? Or maybe some best practices?

1/12/2018 7:18:17 AM

Mikhail Sladkomedov

4 Answers

New Answer

+7

main is just like any other method in your program. You cannot DEFINE other methods inside main, every other method needs to be CALLED inside main either explicitly or implicitly (that way, all your methods get executed). I'd like you to go through this link, just for a better understanding. https://csis.pace.edu/~bergin/KarelJava2ed/ch2/javamain.html

+6

You should not put too much code in the main() method. Rather, you should define classes and functions. This will help you to keep the code manageable. It might seem tough at first, but it'll become easy after you have completed the course.

+3

Remember that the main() method must be in an object with EXACTLY the same name as the Java file. If main() is outside the object the code won't work.

+1

thanks to all!