10

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.

aioobe
  • 413,195
  • 112
  • 811
  • 826
ankit
  • 4,919
  • 7
  • 38
  • 63

1 Answers1

15

Adding final to a static method can actually make a difference. Consider the following code:

class A {
    public static void main(String[] args) {
        System.out.println("A");
    }
}

class B extends A {
    public static void main(String[] args) {
        System.out.println("B");
    }
}

class C extends B {
}

public class Test {
    public static void main(String[] args) {
        C.main(args);  // Will invoke B.main
    }
}

Adding final to A.main would prevent accidental hiding of A.main. In other words, adding final to A.main guarantees that B.main is not allowed, and that C.main therefore prints "A" as opposed to for instance "B".

Why are we allowed to have a final main method in java?

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

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • 3
    `final` will prevent hiding a `static` method. – Sotirios Delimanolis Sep 25 '14 at 15:53
  • I tried adding final to A's main method, the class is now having compilation error in Class B - **Cannot override the final method from A**. I am using Java 1.7. Is this answer valid? – Gyanendra Dwivedi Sep 04 '15 at 14:59
  • @gyan, I don't follow you. Which part do you think is invalid? – aioobe Sep 04 '15 at 21:44
  • @aioobe the current given code is good and compiling. But if I try `public static final void main(String[] args)` in class A; my eclipse is showing compilation error in Class B's main method. The error is - **Cannot override the final method from A** . Because of the error, I am not able to validate your point - *so that C.main in fact prints "A" instead of "B".* Please let me know, if you still do not get it, I will share a screenshot of my eclipse. – Gyanendra Dwivedi Sep 07 '15 at 07:38
  • @gyan, I understand what you're saying, but I'm asking which part of the answer you think is invalid. The answer clearly states that *Adding final to `A.main` would prevent accidental hiding of `A.main`* which seems to be precisely what you've discovered. – aioobe Sep 07 '15 at 07:40
  • 1
    Because of the error, I am not able to validate your point - *so that C.main in fact prints "A" instead of "B".* – Gyanendra Dwivedi Sep 07 '15 at 07:40
  • 1
    If you add `final` to `A.main` you can't have `B.main`, which makes sure `C.main` in fact will print `"A"`. I've reworded the answer slightly to clarify. I don't think the answer is invalid in any way. – aioobe Sep 07 '15 at 07:49
  • Thanks for editing the answer. Now it is more clear. It means adding final to main method at any class will not allow to create a main method in the child classes into the inheritance hierarchy. – Gyanendra Dwivedi Sep 09 '15 at 18:12