• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Concerns regarding the main method

 
Subhashish Pattnaik
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Ranch members, i have had a doubt regarding the overloading of main method in JAVA.

Am not quite sure of this theory in particular and would definitely seek a help from you !!

Is it possible to overload the MAIN method in java??
My apologies if am falling short of some explaination here.


Regards
Subhashish
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the main method is just like any other method, so you can also overload it just like any other method.

However, the JVM will only look for a specific version of the main method that is public, static and void and takes a String[] argument. You can create other methods called main, but these cannot be used as the entry point of your program.
 
Subhashish Pattnaik
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks Jes!!

Can you help me out by an instance of short example. That should help me understand better !!

Hope the master helps me out.

Regards
 
fred rosenberger
lowercase baba
Bartender
Posts: 12084
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there's not much to it...Why don't you try doing it yourself first, and post your attempt here.

Write a class with a "public static void main(String[] args)" method. Then write another main method, with a different argument list. see if it compiles. See if you can get the non-standardly defined one to run from the command line. (hint - you can't).
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another twist to the tale... if you're using JDK 5 or above (which you must be, unless you're stuck with some legacy code), try:



Just for fun! Try to guess if it compiles and/or runs.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic