CrazyEngineers Archive
Old, but evergreen and popular discussions on CrazyEngineers, presented to you in read-only mode.
@ashie • 13 Jun, 2012

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

}

}

@Neeraj Sharma • 13 Jun, 2012 • 8 likes 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 • 28 Jun, 2012 If we are writing two main a class,how will the compiler know which one to call when it runs??
@Anoop Kumar • 28 Jun, 2012
formesush
If 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 • 28 Jun, 2012
formesush
If 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 • 02 Jul, 2012
ashie 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 • 02 Jul, 2012 formesush , 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 • 03 Jul, 2012 no we cant have multiple main class ,no way
@Neeraj Sharma • 04 Jul, 2012
TaraJava
no we cant have multiple main class ,no way
You can have it by overloading. Check the above posts..
@fakirguy • 06 Jul, 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 • 06 Jul, 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 • 15 Jul, 2018 • 2 likes

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:

https://www.youtube.com/watch?v=Qlhslsluhg4

@Vinay Mishra • 23 Jan, 2020 • 1 like

Yes, you can use 'n' numbers of main methods in any class in JAVA. But remember one thing that is, "each class should have only one public static void main(String[]args) method." That means, you can have as many main methods as you like but the main methods can have different signatures from the main(String[]args). This concept in JAVA is known as Overloading.

If you confused in JVM calling of the main method. then let me clarify... JVM will only figure out the main method having the signature, 'public static void main(String[]args)'. Other main methods will be getting ignored by JVM. If you want to call other main methods. then you have to call them from inside of the (String[]args) main method.

@Mohana Chandra • 23 Jan, 2020

Inside a class we can have any number of main methods,but they have to be differ by signatures(means may be difference in order of occurence of arguments ,difference in type of arguments or may be difference in length of arguments ). Because JVM calls the methods based on signature , not on the return type.

5.9k views

Related Posts

@rukawa · Jul 20, 2014

Is there a C/C++ library that offers digital signal processing functions like qpsk modulation?
5.2k views

@Kaustubh Katdare · Feb 6, 2014

Was going through Moto G specs and found that it has a hall effect sensor. Just read about and remembered the 'hall effect' I had studied during my engineering course....
34.4k views

@Ankita Katdare · Nov 5, 2013

ZTE has today launched a new Android smartphone called the ZTE Grand S Flex in Europe. Making the phone available in Spain, Czech Republic and Slovakia followed by Germany, Finland...
8.8k views

@mifo · Sep 4, 2013

Hello everyone, I need an 8051 trainer kit. Since I am almost done with the theory, I think it will be good to prepare projects. So can you guys please...
4.6k views

@parin bumia · Jan 3, 2014

Where to use TPN switch and TPN Isolator,Moreover y to use it.I Have gr8 confusion regarding the realm of neutral wire.y is it so necessary. Plz help.........
5.2k views