Main() method in java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 12

Main() method in java

Should a main() method be compulsorily declared in all java classes?

11th Sep 2018, 10:44 PM
//Fpharbhulourz/>😋😈🤓🇳🇬
//Fpharbhulourz/>😋😈🤓🇳🇬 - avatar
21 Answers
+ 9
main() in all classes: NO main() in application: YES* You need a static main(String[]) method as an entry point of your application (to start the code execution with). If your application has tons of classes, one entry point in one of the classes is more than enough. *If you write a Java library, plugin, addon, or anything not directly runnable code (think of .DLL in windows), then you need no entry point at all. If you write no main() method at all, your code WILL compile just fine. If SoloLearn shows error, it is because it requires an entry point to run from, it is not because of Java. If you are using an IDE (Eclipse, NetBeans, IntelliJ IDEA...) then you can have multiple main() methods and start the application from whichever main() you want. Once you make a runnable .jar file from your Java code, you must specify which main() will be its entry point. So, by default, you don't need main() at all. To run the application with any number of classes, you are required to have at least one main() somewhere.
12th Sep 2018, 10:01 AM
Magyar Dávid
Magyar Dávid - avatar
+ 6
Yes . I think so because without main method your promgram will remain incomplete and the compiler will show error
12th Sep 2018, 12:33 AM
Purab Gupta
Purab Gupta - avatar
+ 6
No 💖💕
12th Sep 2018, 11:11 PM
NimWing Yuan
NimWing Yuan - avatar
+ 4
no its not compulsory to have main , without it your prg will not cause error however it will only compile and will not run as interpretation starts with main ()
12th Sep 2018, 10:19 AM
Nao
Nao - avatar
+ 3
A handy link with answers 💗 https://www.sololearn.com/learn/Java/2137/
12th Sep 2018, 12:46 AM
NimWing Yuan
NimWing Yuan - avatar
+ 3
As a entry point.
12th Sep 2018, 5:06 PM
Nilavarasan
Nilavarasan  - avatar
+ 3
In java MAIN METHOD is strictly necessary because :- public static void main (String args[]) public :- The access of this method is public static :- This method can access from any part of your program so 'static' declared. void :- Method doesn't return anything. main(String args[]) :- To this main method string is passed as a reference argument which objects are are stored in the array[]. <HOPE IT WILL HELP YOU!!!>
13th Sep 2018, 8:36 AM
Amit Kumar
Amit Kumar - avatar
+ 2
The Reason is Ur JRE doesn't recognize any other method except main() method So to run ur program it compulsory to have a main method in all java classes
12th Sep 2018, 9:37 AM
krishna falcon
krishna falcon - avatar
+ 2
Not in all, and most likely only one in entire project.
12th Sep 2018, 3:51 PM
BlazingMagpie
BlazingMagpie - avatar
+ 2
Without main function a program is nothing and it will not work because execution of every program starts with main function.
12th Sep 2018, 5:31 PM
Praveen Soni
Praveen Soni - avatar
+ 2
My 5 cents. Entry point may also be declared as final. like this public static final void main(String ... args) {} will compile
13th Sep 2018, 10:07 AM
Lev Tolstoy SPB
Lev Tolstoy SPB - avatar
+ 2
nope one for a project
20th Sep 2018, 4:59 PM
Aarav Raj
Aarav Raj - avatar
+ 1
ANSWERS: yes beacause i went though java tutorial and it says that you need it links: https://code.sololearn.com/cCd6nsj9MJOq/?ref=app see for yourself
12th Sep 2018, 1:14 AM
Potato Hacker
Potato Hacker - avatar
+ 1
yes, because it is the starting point of our java program, main method and class are very important and necessary in java.
12th Sep 2018, 6:50 AM
Sravya
+ 1
main function is called as a driver function....so you need a driver to drive your vehicle....so driver is most.....
12th Sep 2018, 10:26 AM
SatyaJit Pradhan
SatyaJit Pradhan - avatar
+ 1
yes because a main function is like a key without a key a car wonk start and without a main function compiler shows error
12th Sep 2018, 12:19 PM
Danish Ali
Danish Ali - avatar
+ 1
The main method is compulsory because with out main there will not be any execution of the program
12th Sep 2018, 1:16 PM
Padma Sri
+ 1
What about an app. Don't those have init() instead of main()
13th Sep 2018, 12:14 AM
James D'Angelo
James D'Angelo - avatar
+ 1
If you want to run your code after compilation, you need a main function in at least one of your classes. If you have multiple main functions and compile your code, the IDE will ask you which main function you want to compile as your entry point.
13th Sep 2018, 12:28 AM
Chris
Chris - avatar
+ 1
Yes.
2nd Oct 2018, 11:29 AM
Megha M G
Megha M G - avatar