wood burning stoves 2.0*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Main() Class" Watch "Main() Class" New topic
Author

Main() Class

Sri Addanki
Ranch Hand

Joined: Apr 27, 2001
Posts: 195
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

Joined: Feb 26, 2001
Posts: 4446
    
    5

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

Joined: Feb 26, 2001
Posts: 4446
    
    5

Moving this to Java in General (beginner)
Sri Addanki
Ranch Hand

Joined: Apr 27, 2001
Posts: 195
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

Joined: Jan 13, 2002
Posts: 732
why do you have 2? for what purpose?
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
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.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Aaron O'Brien
Ranch Hand

Joined: May 24, 2002
Posts: 89
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 ]

Aaron O'Brien
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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)


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Main() Class
 
Similar Threads
main in multiple classes
regarding top level classes
Main() Class
NEED HELP IN BASIC JDBC
Why a non-abstract Parent Class can be extended by an abstract Child Class ?