Java Interview Question
Java Interview Question
No, the return type of main() method must be void only. Any other type is not
acceptable.
No, main() method must be declared as static so that JVM can call main()
method without instantiating it’s class.
If you remove ‘static’ from main() method signature, compilation will be
successful but program fails at run time.
Yes, We can overload main() method. A Java class can have any number of
main() methods. But to run the java class, class should have main()
method with signature as “public static void main(String[] args)”. If you do any
modification to this signature, compilation will be successful.
But, you can’t run the java program. You will get run time error as main
method not found.
7.Can we declare main() method as private or protected or with no
access modifier?
No, main() method must be public. You can’t define main() method as private
or protected or with no access modifier.
This is because to make the main() method accessible to JVM. If you define
main() method other than public, compilation will be successful but you will get
run time error as no main method found.
No you can not override main method in Java, Why because main is static
method and in Java static method is bonded during compile time and you can
not
override static method in Java.
you can make main method final in Java. JVM has no issue with that. Unlike
any final method you can not override main in Java.