Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Main() Class

 
Sri Addanki
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
just curious.....How many Main() classes can you have in the application?
I have 2 Main() executable Classes.
Thanks,
Sri
 
Junilu Lacar
Bartender
Pie
Posts: 7772
62
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As many as you want. In fact, all your classes can have main() methods if you so desire(although I don't think it would earn you any points for clarity). The main() method executed will be that of the class you specify on the command line when you start java.
Junilu
 
Junilu Lacar
Bartender
Pie
Posts: 7772
62
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to Java in General (beginner)
 
Sri Addanki
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
My other question was abt having how many Main() classes in the application...
and unfortunately it was moved to Java(beginner's)forum.
I know an application can have any number of main classes, but thats not what i meant.
Based on my Understanding of programming FOR ANY APPLICATION to be good, there can be only ONE main class, or a SINGLE executable class.
And there should be ONLY ONE class through which you can execute or run the application. Then only that application is said to be designed well. right?!
But i had 2 main classes in my application. So i wanted to find out if i was the only one to do that or is someone else doing like me?
Anyway, thanks for the answer,
sri
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why do you have 2? for what purpose?
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As already posted, you can have a main class for every class in your application. It can be used to test the functionality of each class, during development. The key thing is that of all your main classes, only one of them can be public, and this is the one that will drive the application, and the class name must match the name of the file that contains it.
 
Aaron O'Brien
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Bosen! For clarity sake you should only have one main() method or main class...you can get into overloading the main() which is a totaly different method and things can get pretty confusing. The only main() that runs is the main() method that takes a string array! others would usually have to be called with appropriate arguments. You must have one main(with the appropriate array) in your application.
haha too early for me! thanks guys
Aaron - SCJP2
[ May 29, 2002: Message edited by: Aaron O'Brien ]
[ May 30, 2002: Message edited by: Aaron O'Brien ]
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Aaron O'Brien:
You must have one no args main() in your application.
Actually you must have one public static main that takes an array of Strings as an argument. This is the form:
public static void main(String[] args)
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Aaron O'Brien:
You must have one no args main() in your application.

Perhaps you meant: You must have one main method that takes a String array as its only argument.
There is no requirement for a no args main method. In fact, the presence of such a method might lead to some confusion.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic