As we discussed in previous question that we can overload main method. We can change the return type for main method but java compiler will refuse to acknowledge it as entry point for our application.
Example
public class Main { public static String main(String[] args) { System.out.println("main method with "); return "w3schools.com"; } } |
Output
Error: Main method must return a value of type void in class Main, please define the main method as: public static void main(String[] args) |
Java interview questions on main method
- Can we overload main() method in java?
- Can we declare main() method as private or protected or with no access modifier?
- Can we declare main method as non static in java?
- Why main() method must be static?
- Can we change return type of main() method?
- Can we run java class without main() method?
- Can we declare the main method as final?