Is it necessary to use main method in java icse programming | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is it necessary to use main method in java icse programming

I have a doubt regarding writing programs in jave for icse class 10 Can any one tell whether it is necessary to write a main method in each and every program of the question paper in ise examination

23rd Jun 2020, 2:15 PM
MST
3 Answers
+ 8
Is main method compulsory in Java? The answer to this question depends on the version of java you are using. Prior to JDK 7, the main method was not mandatory in a java program. You could write your full code under static block and it ran normally. The static block is first executed as soon as the class is loaded before the main(); the method is invoked and therefore before the main() is called. main is usually declared as static method and hence Java doesn’t need an object to call the main method. When you will give the run command(i.e java Test in the below-mentioned program in notepad), so compiler presumes Test is that class in which main() is there and since compiler load, the main() method, static blocks are ready to get executed. So here, it will run static block first and then it will see no main() is there. Therefore it will give “exception”, as exception comes while execution. However, if we don’t want an exception, we can terminate the program by System.exit(0); ........... Thankyou...........
24th Jun 2020, 3:25 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
Actually these days we are not connected to our teachers, you know corona crisis
23rd Jun 2020, 2:28 PM
MST
+ 1
Thanks bro
25th Jun 2020, 3:37 AM
MST