Can anyone tell me the use of making main method as final in java.
while this is allowed in java
public static final void main(String[] args) {
}
I dont see any use of making it final. anyways it is static so we can not override it.
Can anyone tell me the use of making main method as final in java.
while this is allowed in java
public static final void main(String[] args) {
}
I dont see any use of making it final. anyways it is static so we can not override it.
Adding
final
to a static method can actually make a difference. Consider the following code:Adding
final
toA.main
would prevent accidental hiding ofA.main
. In other words, addingfinal
toA.main
guarantees thatB.main
is not allowed, and thatC.main
therefore prints"A"
as opposed to for instance"B"
.Beside the above corner case, adding
final
to a static method doesn't make much difference, so I don't see a big point in adding a rule for disallowing it.More information available here: Behaviour of final static method