aspose file tools*
The moose likes Beginning Java and the fly likes can there  be  more  than one main method in a java  program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can there  be  more  than one main method in a java  program" Watch "can there  be  more  than one main method in a java  program" New topic
Author

can there be more than one main method in a java program

linus dale
Ranch Hand

Joined: Jul 01, 2009
Posts: 44
because most books say

every java application has to have at least one class and at least one main method

when does it happen
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
Yes, you can have as many main methods as you like. You can have main methods with different signatures from main(String[]), which is called overloading, and the JVM will ignore those main methods.
You can have one public static void main(String[] args) method in each class. Some people use those methods for testing; they can test the operation of each class individually. The JVM will only invoke the public static void main(String[] args) method in the class you name when you write

java MyClass
linus dale
Ranch Hand

Joined: Jul 01, 2009
Posts: 44
thanks

got it
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
You're welcome
Steve Jefferson
Greenhorn

Joined: Jul 09, 2009
Posts: 10
Reply from Linus is appreciated. It cleared my doubt too in this question.


Project Management Tool | Automatic Scheduling
Rohan Kadbe
Greenhorn

Joined: Jun 05, 2010
Posts: 3
but if we write same signature as compiler/jvm would invoke.......




can we tell compiler which main to use as an entry point....so that I can change entry point as I like....
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19557
    
  16

Those two methods have the same signature. The only way to have two main methods is by having two different classes each with one main method. The name of the class you use to invoke the JVM (e.g. java Class1, java Class2) determines which main method is called.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
Because you have two methods with the sam signature, your TwoMain class wouldn't compile.


SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
And welcome to JavaRanch
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10929
    
  12

Rohan Kadbe wrote:but if we write same signature as compiler/jvm would invoke.......

can we tell compiler which main to use as an entry point....so that I can change entry point as I like....

Even if this did compile, how would you tell the compiler which one to invoke? Remember, all you type is

java TwoMain param1 param2 param3...

inside the compiled code, it probably doesn't even use the string "args1" or "args2".


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rohan Kadbe
Greenhorn

Joined: Jun 05, 2010
Posts: 3


@fred rosenberger

correct.
that is my question that can we tell compiler which main to use as an entry point....
as method signatures are same is there any way that compiler leaves/hides/ignore the first main and take entry point as second main

this code won't compile i know but can we correct it in some way..........



@Campbell Ritchie
thanks.....cheers
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2970
    
    9
Rohan Kadbe wrote:that is my question that can we tell compiler which main to use as an entry point....

No, you can't. The code won't compile.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19557
    
  16

I already gave the correct answer - you'll need two classes each with a main method.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10929
    
  12

Rohan Kadbe wrote:

@fred rosenberger

correct.
that is my question that can we tell compiler which main to use as an entry point....
as method signatures are same is there any way that compiler leaves/hides/ignore the first main and take entry point as second main

this code won't compile i know but can we correct it in some way..........

You don't tell the compiler where to enter the code. One of the first things the compiler does is check to see if your syntax is valid. Your example is not. You cannot have ANY two methods with the same signature, main or otherwise, because they cannot be distinguished.

the only way to correct it is to a) change the name of one of the methods, b) create a second class and put one main in each, or c) create a new main method that takes a param from the command line, and use that to determine which of the two methods you want to run (although you still have to re-name them).
Rohan Kadbe
Greenhorn

Joined: Jun 05, 2010
Posts: 3
thanks...all ....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can there be more than one main method in a java program
 
Similar Threads
What is mandatory in a class?
Is the main method inherited??
private class
Thread vs Process
Looking for a logic to insert delay in our java code