What should and shouldn't I include in "main" method in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

12th Jan 2018, 7:18 AM
Mikhail Sladkomedov
Mikhail Sladkomedov - avatar
4 Answers
+ 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
12th Jan 2018, 8:23 AM
Dev
Dev - avatar
+ 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.
12th Jan 2018, 7:33 AM
DAB
DAB - avatar
+ 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.
12th Jan 2018, 8:24 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 1
thanks to all!
12th Jan 2018, 8:26 AM
Mikhail Sladkomedov
Mikhail Sladkomedov - avatar