• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

can a class or a java file have more than one "main "method?

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes people i am curious to know this thing that, can a class or a java file have more than one "main "method?

if you have "yes answer", please provide me how(only the way,no need of exact code), and tell me also that how it is possible while i have read in many books that a .java file can have only 1 "main" method.
 
Marshal
Posts: 80055
409
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean, can you overload a main method? Of course you can. But the JVM will only call the version with public static void main(String[] args) as its header.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by tomendra singh:
yes people i am curious to know this thing that, can a class or a java file have more than one "main "method?

if you have "yes answer", please provide me how(only the way,no need of exact code), and tell me also that how it is possible while i have read in many books that a .java file can have only 1 "main" method.



What do you actually want to do? If you want different code to be executed on application start you can usually do this by using command line arguments.
 
Tomendra Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have not understand you luke, i am a beginner please tell me what does that mean.

i am interested in that can we have more than one main method in a class or in a .java file?
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like Campbell said, you can have more than one method with the name 'main', but only one - public static void main(String[] args) - will get called when you try to execute the application.

So my question is, are you asking just to have the question answered (i.e. an exam question/understand some basic confusion), or are you trying to actually solve a problem in your code. If you want to solve a problem, what is the problem you want to solve?
 
Tomendra Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you luke,i understand what you and campbell have said.
 
Tomendra Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you tell me that how can we override main method?
please provide me any example with code if you can.

thank you.
 
Campbell Ritchie
Marshal
Posts: 80055
409
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 80055
409
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, it's overload, not override.
 
Tomendra Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much campbell.i understand.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't mean to hijack the thread, but I've never seen this before. I typically overload the constructor and other methods, but why would you overload main() and how is it useful?
 
Campbell Ritchie
Marshal
Posts: 80055
409
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I often think there is an all-purpose do-it-yourself excuse which excuses absolutely everything in computing, "I wanted to see what happened if . . ."
Examples of programming diversions that can cover are
  • "I wanted to see what happened if . . ." I delete that file from the Windows directory.
  • "I wanted to see what happened if . . ." I pour coffee onto the keyboard.
  • "I wanted to see what happened if . . ." I overload my main method.
  • At least the last of the three can't do any significant damage.

    But it is an exercise of about that same level of uselessness!
     
    Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic