• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
linus dale
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks

got it
 
Campbell Ritchie
Sheriff
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Steve Jefferson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reply from Linus is appreciated. It cleared my doubt too in this question.
 
Rohan Kadbe
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you have two methods with the sam signature, your TwoMain class wouldn't compile.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
fred rosenberger
lowercase baba
Bartender
Posts: 12123
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Rohan Kadbe
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


@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
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already gave the correct answer - you'll need two classes each with a main method.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12123
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks...all ....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic