aspose file tools*
The moose likes Beginning Java and the fly likes main() methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "main() methods" Watch "main() methods" New topic
Author

main() methods

sai donthneni
Ranch Hand

Joined: Mar 26, 2006
Posts: 50
I am a beginner and I have question. Can we put 2 main() methods in a single java file.
for eg:

class A
{
public static void main(String )
{

}
}

class B
{
public static void main(String args )
{


}

}
Martin Simons
Ranch Hand

Joined: Mar 02, 2006
Posts: 196
in endd effect main is a method like any other, however, since you
will only have one public class in a file I believe the JVM will only
be able to execute the main method from the public class making the
main method in the other class irrelevant.
ak pillai
author
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
Why do you want to do that. It is a good practice to have one class per one file.


java j2ee job interview questions with answers | Learn the core concepts and the key areas
sai donthneni
Ranch Hand

Joined: Mar 26, 2006
Posts: 50
Thanks Martin and Pillai for your help.
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
You most certainly can and it will work just fine (meaning that after you compile running java A will run the A's main method and running java B will run the B's main method). It is however, one of, if not THE, most unadvised thing(s) to do according to the java coding conventions.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Martin Simons:
...I believe the JVM will only be able to execute the main method from the public class making the main method in the other class irrelevant.

No, you can execute main from a class with default access, so one file can contain as many class definitions with main methods as you like. (But only one public, of course.)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Emanuel Kadziela:
...It is however, one of, if not THE, most unadvised thing(s) to do according to the java coding conventions.

Really? I've read that it's actually a recommended approach to faciliate testing during the development process. In fact, Bruce Eckel advocates this in Thinking in Java...
You can create a main( ) for each one of your classes, and it's often recommended to code this way so that your test code is wrapped in with the class. Even if you have a lot of classes in a program, only the main( ) for the class invoked on the command line will be called. (As long as main( ) is public, it doesn't matter whether the class that it's part of is public.) ... This technique of putting a main( ) in each class allows easy unit testing for each class. And you don�t need to remove the main( ) when you�re finished testing; you can leave it in for later testing.

Are you referring to these Java coding conventions from Sun? I've skimmed these, but I didn't see anything on this topic.
[ March 28, 2006: Message edited by: marc weber ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Let's back up and see if we got the question right.

First, you can only put one top level class in each file. You can put nested classes in the same file, but you'll find nested classes can't have static methods, so no static main(). So you cannot have two classes with the standard main signature in one file.

Second, you can have a main on every class in your system. Before JUnit came along and stole our hearts this was a commmon way to write snippets of test code. Nowadays I'd rather have JUnit tests, but main for test and demonstration is still valid enough.

Third, it's probably bad form to call main() on another class. You can certainly do it, but it implies something missing in the other class design. It was designed to be called from the command line and you're calling it from another program. I've actually done this because it was the easy way to front-end a command-line utility with some setup and customization, and it definitely said to me the author of the utility failed to design an API I could use.

Last for now, you could overload main with different signatures and put several main() methods on one class. That would be very confusing to future readers which is a Bad Thing. The JVM will only call the exact signature that we all code. Any others would just be distracting.

Any of that hit the right spot?

--------

Try to compile & run this and see what happens:


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Stan James:
Let's back up and see if we got the question right.

First, you can only put one top level class in each file. [/CODE]


This is incorrect. You can put as many top level classes in a file as you wish. However, you can only have one public top-level class in the file. All of the classes can have a main() method. There really is nothing special about main() in and of itself. However, the JVM will only be able to run the main method in the public class.

Layne


Java API Documentation
The Java Tutorial
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Interesting correction. I will have to go play with that!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

To take this a bit further...
  • You can have at most one (although you don't need any) top-level public class OR interface in a file.
  • If you do have a top-level public class or interface, then the file name must match that exactly.
  • If you don't have any top-level public class or interface definitions, then the file name can be anything -- i.e., it does not need to match any of the class or interface names (in which case you would compile using the file name, but run using the class name).
  •  
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: main() methods
     
    Similar Threads
    overloading main method
    can't run
    is it wrong to use "this" in static methods?
    referenc static variables with this in methods
    Please Help me out...