• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

main class

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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[])
...............
}
}
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
     
    Ranch Hand
    Posts: 127
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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 ]
     
    lowercase baba
    Posts: 13089
    67
    Chrome Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    This tiny ad is wafer thin:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic