+ 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?
6 Antworten
+ 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
+ 3
Mohammad Yahya
Thanks for the making it clear with the ease....
+ 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?
+ 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.
+ 1
Jain Rishav Amit, Yes, exactly. But the main method should be static.
+ 1
Thanks for the good question, because I also learned something new..🙋