Why main method can't be final in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why main method can't be final in java?

As there is no purpospe and also not possible to override main method in java, why can't we make it final?

2nd Jul 2018, 4:05 AM
Jain Rishav Amit
Jain Rishav Amit - avatar
6 Answers
+ 3
Hi Jain Rishav Amit ,Jay Matthews , The entry point for a java application is the main method. But STATIC MODIFIERS ARE IMPLICITLY FINAL. The final keywords is used for methods. They cannot be overridden. Similarly static method cannot be overridden. So the final modifier along with the static modifier will not have any change in the main method. They are the same. Please don't have a narrow notion that "public static void main(String args[])" is the entry point of the application. Just remember that the main method is the entry point. You may ask that if static and final are same, then i can aswell declare the main method like this: "public final void main(String args[])"? The prg. will compile, but will give you an error. Because the JVM check's for the main mehtod in the program along with the static modifier. public static void main(String args[]) = public final static void main(String args[]) IT DOES NOT CHANGE THE METHOD. HOPE THIS HELPS. Source: https://coderanch.com/mobile/t/1983
2nd Jul 2018, 4:34 AM
Root
Root - avatar
+ 3
Mohammad Yahya Thanks for the making it clear with the ease....
2nd Jul 2018, 4:48 AM
Jain Rishav Amit
Jain Rishav Amit - avatar
+ 2
Mohammad Yahya In short, static can not be overriden, so there is no use of making main method final, as overriding is already being prevented by static keyword.. is it so?
2nd Jul 2018, 4:43 AM
Jain Rishav Amit
Jain Rishav Amit - avatar
+ 1
Static is needed for main Method, In order to JVM be able to achieve the main method without making the object of the class otherwise it can not access the main(entry point) and program don't run.
2nd Jul 2018, 4:41 AM
Root
Root - avatar
+ 1
Jain Rishav Amit, Yes, exactly. But the main method should be static.
2nd Jul 2018, 4:44 AM
Root
Root - avatar
+ 1
Thanks for the good question, because I also learned something new..🙋
2nd Jul 2018, 5:00 AM
Root
Root - avatar