answersLogoWhite

0

Is it possible to overload the main methode in java?

Updated: 8/19/2019
User Avatar

Wiki User

12y ago

Best Answer

Yes. The main method can be overloaded just like any other method in Java.

The usual declaration for main is: public static void main(String[] args) throws Exception; When you launch a java application it looks for a static method with the name 'main', return type 'void' and a single argument of an array of Strings. ie what you throw is unimportant in resolving this method. Overloading is providing multiple methods with the same name but different arguments (and potentially return type). The following class is valid: public class Test { public static void main(Object[] o) { } public static boolean main(String[] o) { } } But when run will result in the error: Exception in thread "main" java.lang.NoSuchMethodError: main Since the methods present does not have the correct signature.(The main method with the void return type is missing. Without it the code cannot be executed by the JVM)

Overloading is sometimes confused with overriding. Overriding is changing the behavior of a method in a subclass. This is not allowed on a main method because main must be static and is therefore bound at compile time, not run time, e.g., the program: class A { static String foo() { return "A"; } } class B extends A { static String foo() { return "B"; } } public class Test { public static void main(String[] args) { A a = new B(); System.out.println(a.foo()); System.out.println(((B)a).foo()); } } Prints: A B This is why it is generally discouraged from calling static methods on instance variables. You should always write A.foo() or B.foo() as this reminds you that there is no polymorphic behavior occurring.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

Yes

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Is it possible to overload the main methode in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Is it possible to define a java static method inside a main method?

Because, the main method is the starting point of the java program and if we need an object of that class even before the main can be invoked, it is not possible. Hence it is declared static so that the JVM Can acess the main method without having to instantiate that particular class


Why main is used in java?

In java need main() method. without main() in java we won't run the java programe main() signals the entry point to the program - it tells the Java Virtual Machine which is the first program that should be executed.


Who invoke main function in java?

The Java Runtime Environment invokes main methods.


Which api used in java?

The Java API is the API for the main Java Library.


What is the main contraindication to epogen therapy?

iron overload


How do you call main class with in main class in java?

We can't call a class. We always call a method in java.


Why do you have to install java to play RuneScape?

because runescape main engine is java


What are the main cities in java?

supre


What are the main landforms of Jakarta?

Jakarta is in the island of Java. Java is part of Indonesia archipelago


What is the main difference between UNIX and JAVA?

Unix is an operating system, Java is a language.


How do you print message before main in java?

You cannot do that. The main method of a java class is the point where the execution begins. You can print messages only after a main method is invoked.


What will happen if a Java Class has no Main Method?

Nothing will happen. There is no restriction that every Java class must have a main method. The only program is that, this class cannot be executed as a standalone java program.