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%)
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.
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.
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?
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?
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!