can we use 2 main on 1 class in java?
public class test{
int j;
public void show();
public void see()
public static void main(string args[])
{
test test=new test();
test.show();
}
public static void main(string args[])
{
test.see()
}
}
Member • Jun 13, 2012
class MultiMain { public static void main(String[] args) { main(1); main('a'); } public static void main(int i) { System.out.println("Integer main"); } public static void main(char a) { System.out.println("Char main"); } }In this way you can use multiple mains inside a single class in Java
Member • Jun 27, 2012
Member • Jun 27, 2012
It will take only this public static void main(String[] args)formesushIf we are writing two main a class,how will the compiler know which one to call when it runs??
Member • Jun 28, 2012
Compiler never decides run time execution. It just check whether the program written is semantically and syntax wise correct or not. According to Java semantics you cannot have a method with exactly same signature twice right? Hence compiler throws the error.formesushIf we are writing two main a class,how will the compiler know which one to call when it runs??
Member • Jul 2, 2012
Member • Jul 2, 2012
Member • Jul 2, 2012
Member • Jul 3, 2012
You can have it by overloading. Check the above posts..TaraJavano we cant have multiple main class ,no way
Member • Jul 5, 2012
Member • Jul 5, 2012
the Java compiler (java.exe)fakirguy. When you are executing your program, the Java compiler (java.exe) looks for public static void main(String[] args) in the class you ask it to execute.
Member • Jul 15, 2018
The answer is Yes, but you should consider the following 3 points.Â
1. No two main method parameter should be the sameÂ
Eg. public static void main(int i)Â
public static void main(int i, int j)Â
public static void main(double j)Â
public static void main(String[] args)Â
2. Javaâs actual main method is the one with (String[] args), So the Actual execution starts from public static void main(String[] args), so the main method with (String[] args) is must in a class unless it is a child class.Â
3. In order for other main methods to execute you need to call them from inside the (String[] args) main method.Â
Here is a detailed video:
<a href="https://www.youtube.com/watch?v=Qlhslsluhg4" target="_blank" rel="nofollow noopener noreferrer">- YouTube</a>