Java Main()

c_user's Avatar, Join Date: Aug 2009
Contributor
Hi frds,
can there be more than one main() method in a single program.
please justify.
thanking you
0
guptasandeep's Avatar
Banned
yes there can be more than one main method in a single program but in different class.
0
miteshaegis's Avatar, Join Date: Nov 2012
Newbie Member
Hi,

You can use more than one main method in different class.

thanks
0
ManzZup's Avatar, Join Date: May 2009
Skilled contributor
if you are asking about overloads yes
0
ArnavKumar's Avatar, Join Date: Oct 2013
Newbie Member
The number of main methods can not be greater than number of classes. Each class can have its own main method. But a class can not have more than one main method.
0
ManzZup's Avatar, Join Date: May 2009
Skilled contributor
a class can, the jvm looks only for the one that matches the correct format
rest it ignores and throw the NoMainMethodFound kinda exception if you try to run it
0
ArnavKumar's Avatar, Join Date: Oct 2013
Newbie Member
Code: java
public class FirstMain {

    protected void main(){

    }
    public static void main(String[] args) {
        System.out.println("in first main class");
    }
}

class SecondMain{
       
    private int main(int a){
        return a;
    }
   
    protected void main(){

    }
    public static void main(String[] args) {
        System.out.println("in second main class");
    }
}

class ThirdMain{
    protected void main(){

    }
    public static void main(String[] args) {
        System.out.println("in third main class");
    }
}
Here you are having different mains in different classes. Are you looking for this?
shabbir like this