Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

main class

 
anita dhar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it possible to declare more than one main() in the prorgram . and when we compile the program how compiler will come to know what is the main()progrom
class abc
{
public static void main(String args[])
{
.....
}
}

class xyx
{
public static void main(String args[])
...............
}
}

class rrr {
public static void main(String args[])
...............
}
}
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your example is a bit different from what you are saying
You are showing three classes, with a main method in each. There is no problem, because you have to tell the jvm which one is the main class.
"java xyz" will launch the xyz class.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. You can have.

But while running you need to give the corresponding class name whose main() method you want to invoke.

See the following program:



While running the program,

  • "java TestPgmHaving2MainMethods" - invokes the main() method present in the TestPgmHaving2MainMethods class
  • "java SecondPgmHavingAMainMethod" - invokes the main() method present in the SecondPgmHavingAMainMethod class



  • Make sure to give the package name while running if the program is NOT in the default package!!
     
    Vilas Lawande
    Ranch Hand
    Posts: 127
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Program name must be class name -- rule in Java
    if you give any class name to your program.
    Then it must work fine.
     
    Anil Kumardvg
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You are telling all main methods are in separate class know.But they are in one java file.
    when you save the file you have to mention the class name which main method has to execute first.compiler first look at that class file for main method then execute the body of the main() method.
    you can call the main method of the other classes from main() method currently exucuting. you are using static main method so you can call main method by using class name.

    go throw this example
    save file as abc.java

    Three class file will generate which ever you call at the run time that will execute first

    class abc
    {
    String a[]={"a","b"};
    public static void main(String args[])
    {
    String a[]={"a","b"};
    System.out.println("hi");
    xyx.main(a);
    }
    }

    class xyx
    {
    public static void main(String args[]){
    System.out.println("hi");

    }
    }

    class rrr {
    public static void main(String args[]){
    System.out.println("hi");
    }
    }
    [ May 25, 2007: Message edited by: Anil Kumardvg ]
     
    fred rosenberger
    lowercase baba
    Bartender
    Pie
    Posts: 12098
    30
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You can have as many main() methods in a single class as you want, just like i can have as many foo() methods in a class. Just like any other method, each must have a different signiature.

    However, only the "void public static main (String [] args)" method can be called from the command line. That one is always the starting point. Once your program is running, you can call the other versions of main just like any other method.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic