• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

main() methods

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 )
{


}

}
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 288
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you want to do that. It is a good practice to have one class per one file.
 
sai donthneni
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Martin and Pillai for your help.
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.)
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting correction. I will have to go play with that!
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
  •  
    machines help you to do more, but experience less. Experience this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic