Can we declare main() as final?

Questions by naveen.chinthala   answers by naveen.chinthala

Showing Answers 1 - 69 of 69 Answers

guna

  • Feb 21st, 2012
 

If we declared as final in main,it compiles without error but throws runtime exception as "java.lang.NoSuchMethodError:main"

  Was this answer useful?  Yes

sampra

  • Mar 6th, 2012
 

Yes, main can be declared as final. Final means the method or variable cannot be modified in sub-class It does not have any impact on main because Static function is not a part of object instance.

Code
  1. package com.san;

  2.  

  3. class Base {

  4.         public final void doSomething(Object o) {

  5.                 System.out.println("Object");

  6.         }

  7. }

  8.  

  9. class Derived extends Base {

  10.         public void doSomething(Integer i) {

  11.                 System.out.println("Int");

  12.         }

  13.  

  14. }

  15.  

  16. public class Test {

  17.         public static final void main(String[] args) {

  18.                 Base b = new Base();

  19.                 Derived d = new Derived();

  20.                 b.doSomething(new Integer(0));

  21.                 d.doSomething(new Integer(0));

  22.         }

  23. }

manas

  • Jul 7th, 2012
 

As main is static we cant declare final.

  Was this answer useful?  Yes

visal Sigh

  • Oct 26th, 2012
 

Yes we can.

  Was this answer useful?  Yes

smarti

  • Aug 29th, 2013
 

there will be no exceptions occur while using final keyword in main i check it with my editplus editor by writing
simple java program

  Was this answer useful?  Yes

Indrajeet Kumar Singh

  • Sep 12th, 2013
 

Yes, We can But we need to follow the order of specifier.

  Was this answer useful?  Yes

megha

  • Mar 3rd, 2015
 

there is no axception occure while using final with main method.........

  Was this answer useful?  Yes

Devendra

  • Sep 29th, 2015
 

Smarti please check your JDK version. Your answer is depends on your version. If you have JDK 1.8 then it will show compile time exception else your program get compile as you compiled.
Hope you got your answer.

  Was this answer useful?  Yes

kunal

  • Jan 25th, 2016
 

We can declare main() as final but it will not be the "main" that JVM will be searching. since JVM search for "public static void main(string[] args)" so if you will put anything extra in this syntax then JVM will not be able to find the main method and it will give run time exception.

  Was this answer useful?  Yes

baban

  • Feb 11th, 2016
 

Yes we can declare a main() as final

  Was this answer useful?  Yes

As we know we cannot override a static method hence this final will be redundant. Yes we can declare main method as final. Sure, it can be declared final! It doesnt matter if it is declared final, the JVM will still find it and run it, and the compiler doesnt care

  Was this answer useful?  Yes

Sai Ram

  • Apr 2nd, 2016
 

Compile succeeds, but will result in Run time exception
Exception in thread "main" java.lang.NoSuchMethodError: main

  Was this answer useful?  Yes

Debashis Nanda

  • Apr 22nd, 2016
 

Yes, we can declare main method as final no compile time error and no run time error.
But if we can not override that main method in subclass so if we want to execute the subclass, so we have to invoke everything inside static block of subclass.

  Was this answer useful?  Yes

Palash khatri

  • May 4th, 2016
 

There is no such type of error is coming neither in compiling nor in running

  Was this answer useful?  Yes

Amit Sharma

  • Jun 26th, 2016
 

It will work fine, and doesn't throw any error at compile and runtime time.

  Was this answer useful?  Yes

CHANDRA SEKHAR DASH

  • Aug 28th, 2016
 


It will work fine, and doesnt throw any error at compile and runtime time.
Example:

Code
  1. public class FinalInMain {

  2.         final public static void main(String args[]){

  3. System.out.println("Working fine without any error by adding final in main()");

  4. }

  5. }

  Was this answer useful?  Yes

Ganga_busi

  • Mar 3rd, 2017
 

static itself is a public and final, then there is no difference if you are creating main method with final keyword.

  Was this answer useful?  Yes

M.Vijayapandiyan

  • Mar 8th, 2017
 

Clearly explain for why and what purpose do we use the static in main method?

  Was this answer useful?  Yes

jayanth

  • Mar 14th, 2017
 

If a method is declared as final, it means that we cant override the method. So, We can declare main method as final because there will be only one main method in our application that JVM calls (declaring another such method with the same method signature results in a Compile time error and hence there wont be any other such method)

  Was this answer useful?  Yes

k.swathireddy

  • Mar 24th, 2017
 

We cant declare a main as final

  Was this answer useful?  Yes

Skk

  • Apr 10th, 2017
 

You are missing static keyword in main method, that is why error is there.
public static final void main(String[] args) {
}

  Was this answer useful?  Yes

Jyoti Mittal

  • Jun 9th, 2017
 

yes we can

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions