can we use 2 main on 1 class in java?

ashie
@ashie-F59bNm โ€ข Oct 26, 2024

is it valid or it gave compilation error

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()
}
}

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Neeraj Sharma

    @neeraj-iAaNcG • Jun 13, 2012

    You can write a program with multiple main in a single class in java but the above program will give error. To be able to use multiple main methods inside a class, you must overload the main method. In the above program both the main methods have same arguments so it won't execute.
    The following program will work though:

    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

  • formesush

    @formesush-ul2kKR • Jun 27, 2012

    If we are writing two main a class,how will the compiler know which one to call when it runs??

  • Anoop Kumar

    @anoop-kumar-GDGRCn • Jun 27, 2012

    formesushIf we are writing two main a class,how will the compiler know which one to call when it runs??

    It will take only this public static void main(String[] args)
    method signature as starting point....

  • sookie

    @sookie-T06sFW • Jun 28, 2012

    formesushIf we are writing two main a class,how will the compiler know which one to call when it runs??

    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.

    i think in this case there is no need to got upto run time execution.

  • Vikash Verma

    @vikash-verma-jF0MOy • Jul 2, 2012

    #-Link-Snipped-# first of all, your program is syntactically wrong... the signature of main method must be one of the following...โ€‹public static void main(String[] args)โ€‹ โ€‹public static void main(String... anything)โ€‹
    you can only overload main method i.e. parameters in the main method must be different,but the program will execute iff one of the above two main method is present...๐Ÿ˜Ž

  • Vikash Verma

    @vikash-verma-jF0MOy • Jul 2, 2012

    #-Link-Snipped-# , two main method with same signature can never exist in a java program, also it is the JVM which always calls the main method and not the java compiler... ๐Ÿ˜Ž

  • TaraJava

    @tarajava-fqIQFu • Jul 2, 2012

    no we cant have multiple main class ,no way

  • Neeraj Sharma

    @neeraj-iAaNcG • Jul 3, 2012

    TaraJavano we cant have multiple main class ,no way

    You can have it by overloading. Check the above posts..

  • fakirguy

    @fakirguy-zf9M35 • Jul 5, 2012

    I agree with Vikash Verma. It is important to note that the Java compiler will never call the main() method. Java compiler (javac.exe) is not even in the picture when you are executing your program. 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.

  • Anoop Kumar

    @anoop-kumar-GDGRCn • Jul 5, 2012

    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.

    the Java compiler (java.exe)
    Please correct here java.exe is JVM invoker which load class file and run it.
    javac.exe (on windows machine) is java compiler invoker.

  • Charan Raj

    @charan-Dc8bWR • 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>