Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Tomendra Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • 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.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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
  • 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: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thank you very much campbell.i understand.
 
Brian Lang
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • 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!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic