File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes main class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "main class" Watch "main class" New topic
Author

main class

anita dhar
Ranch Hand

Joined: May 24, 2007
Posts: 34
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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!!


    Everything has got its own deadline including one's EGO!
    [CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
    Vilas Lawande
    Ranch Hand

    Joined: Nov 07, 2006
    Posts: 127
    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

    Joined: May 21, 2007
    Posts: 15
    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

    Joined: Oct 02, 2003
    Posts: 10916
        
      12

    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.


    There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: main class
     
    Similar Threads
    Mughal's Mock Exam-Main method declaration
    Signature of main() method
    Inner class and this ?
    can't run
    JAVAC not found ??? I cant compile - help