+ 6

[Duplicate]Is the main method first to be read?(in java)

even if there are other methods before it is the main method first to be read??

15th Mar 2018, 2:55 PM
Neelarghya Kundu
Neelarghya Kundu - avatar
6 Answers
+ 11
Yes, main method is the entry point for all programs.
15th Mar 2018, 3:03 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 9
I answered another question similar to this where I explain why the static main() method is called and how this could be changed to use a different named method or even an instance method. -------------- Technically speaking, a custom java launcher could be created to accept a few additional arguments to override the default application entry point specifying an instance method of a class with a different method name. The JDK java launcher ( /usr/bin/java ) is actually written in C - which may be surprising to newer Java programmers. The java launcher uses the JNI (Java Native Interface) to explicitly set the main() entry point: mainID = (*env)->GetStaticMethodID(env, mainClass, "main", ... ); Essentially, this JNI method returns the ID for a static method to invoke by the name of "main". Alternatively, this could have been: mainID = (*env)->GetMethodID(env, loaderClass, loaderMethod, ... ); ...where the loaderMethod is string variable containing the name of an instance method of the specified class.
15th Mar 2018, 4:58 PM
David Carroll
David Carroll - avatar
+ 6
Yes. In java (and many other languages) , main method is the first to be executed. If there is no main method , the code will not run.
15th Mar 2018, 3:03 PM
cHiRaG GhOsH
cHiRaG GhOsH - avatar
+ 6
Java like most compilers uses a start function that handles uncaught exceptions and static initializations before calling your main function. Therefore, your code can have other functions run prior to main, when they are used to initialize the data. https://code.sololearn.com/cchtaJdt5CZa https://www.sololearn.com/Discuss/813015
15th Mar 2018, 4:34 PM
John Wells
John Wells - avatar
+ 5
thank you very much @chirag ghosh& @shamima yasmin
15th Mar 2018, 3:04 PM
Neelarghya Kundu
Neelarghya Kundu - avatar
+ 4
thank you sir.but can you tell me why this is not working. It ran in my computer https://code.sololearn.com/c7EyO5Cc4of0/?ref=app but the output is starting from 8.
15th Mar 2018, 6:30 PM
Neelarghya Kundu
Neelarghya Kundu - avatar