wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes can a class or a java file have more than one Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can a class or a java file have more than one "main "method?" Watch "can a class or a java file have more than one "main "method?" New topic
Author

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

Tomendra Singh
Ranch Hand

Joined: Jun 28, 2008
Posts: 47
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.


Strength Is Life,Weakness Is Death- Swamy Vivekanand.
SCJP 5.0(87%)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38046
    
  22
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

Joined: Jan 28, 2003
Posts: 4167
    
  21

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.


Steve
Tomendra Singh
Ranch Hand

Joined: Jun 28, 2008
Posts: 47
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

Joined: Jan 28, 2003
Posts: 4167
    
  21

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

Joined: Jun 28, 2008
Posts: 47
thank you luke,i understand what you and campbell have said.
Tomendra Singh
Ranch Hand

Joined: Jun 28, 2008
Posts: 47
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

Joined: Oct 13, 2005
Posts: 38046
    
  22
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38046
    
  22
By the way, it's overload, not override.
Tomendra Singh
Ranch Hand

Joined: Jun 28, 2008
Posts: 47
thank you very much campbell.i understand.
Brian Lang
Ranch Hand

Joined: Oct 21, 2008
Posts: 43
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

Joined: Oct 13, 2005
Posts: 38046
    
  22
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!
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: can a class or a java file have more than one "main "method?
     
    Similar Threads
    class and public
    public modifier for inner class
    Can more that one class have main() method in a single source File?
    classes
    doubt if no public class in file whats file name