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
guptasandeep's Avatar
Banned
yes there can be more than one main method in a single program but in different class.
miteshaegis's Avatar, Join Date: Nov 2012
Newbie Member
Hi,

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

thanks
ManzZup's Avatar, Join Date: May 2009
Skilled contributor
if you are asking about overloads yes
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.
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
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