Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

can 2 classes have two "main" method ?

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can 2 classes have two "main" method ?
dont say no try it out !
its working , but how this is possible ?
any one can explain in detail ?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might have two main methods, but I bet you've only got one main(String[]) method. The JVM is looking for a very specific signature: public static void main(String[]). Anything else might be a valid method, but doesn't have any special meaning.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naved momin wrote:dont say no try it out !

is that sarcastic?

naved momin wrote:its working , but how this is possible ?

Post the code that works
 
lowercase baba
Posts: 13018
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly would be the problem with it? A class can have multiple toString() methods, or multiple calculate() methods, as long as they have different signatures.

main() is no different.
 
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course you can declare a lot of main methods (with the signature public static void main(String[] args) ) in a lot of classes. Otherwise we would all have to call "The Class To Start Them All" when we want to run our program.

However, you specify ONE and ONLY ONE class with a main method when you start the program. Even if you declare a main-method in another used class, it will be ignored.

Consider this two classes:





If you run this with:
java Teacher
and
java DummyStudent

you get similar printouts but not exactly. Two different executions has been made.


Not the best way to code, but it works... ;)
 
Marshal
Posts: 74637
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look up overloading in the Java™ Language specification. It's not easy to understand, and you will have to be quick while the web link still works
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic