why is main() static

why do we have to declare main() method of java as 'public' and 'static'?
sumabalaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vivexpCommented:
Hi pal,

The signature for the main() method always looks like this:

     public static void main(String args[]) {...}

Here's a run-down of the parts of the main() method:

     public means that this method is available to other classes and objects. The main() method must be declared public.
     static means that this is a class method.
     void means that the main() method doesn't return anything.
     main() takes one parameter: an array of strings. This argument is used for command-line arguments, which you'll learn about in the next section.

The body of the main() method contains any code you need to get your application started: initializing variables or creating instances of any classes you may have
declared.

When Java executes the main() method, keep in mind that main() is a class method-the class that holds it is not automatically instantiated when your program
runs. If you want to treat that class as an object, you have to instantiate it in the main() method yourself

Helper Classes

Your Java application can have only one class, or, in the case of most larger programs, it may be made up of several classes, where different instances of each class
are created and used while the application is running. You can create as many classes as you want for your program, and as long as they are in the same directory or
listed in your CLASSPATH, Java will be able to find them when your program runs. Note, however, that only the one jumping-off class, only the class you use with the
Java bytecode interpreter needs a main() method. Remember, main() is used only so that Java can start up the program and create an initial object; after that, the
methods inside the various classes and objects take over. While you can include main() methods in helper classes, they will be ignored when the program actually
runs.

Hope it is clear...

Bye
0
expertmbCommented:
hi,
 informative  explaination
mb...
0
shaveriCommented:
If you want to call main() directly from the JRE then it must be both static and public (public so that the runtime can call it and
static so that it can construct instances of the classes to be used). However, there is nothing particularly special about the name main and main() methods can be constructed and used that  take different parameters or leave off the static modifier. This is why the compiler does not assume all main() methods to be static. The following is perfectly legitimate and compilable code - you just can't invoke it from the command line:
                                 
 public class NonStaticMain{
   public void main() { System.out.println("Cannot be called from command line"); }
public void main(String[] args) { System.out.println("Cannot be called from command line, either"); }
 }
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

shaveriCommented:
for more information visit the  foll..site
http://forum.java.sun.com/forum?13@164.7AF8aOF6kmy^0@.ee826cb/0
0
g_senthilkumarCommented:
hi,
   main() is the method from which all the java programs starting the execution.
   public-stands for that main() method is calling by JVM ie outside of the program.So it should be public otherwise the JVM cannot find the main() method.
   static-stands for JVM is going to call the main() method without creating the object of your class.
   void-is nothing but return type of main method.
   main(String []args)-this is nothing but argument list for the main method that is predefined one.So if u simply give main() without arguments then JVM cannot understand your main() method and it will simply give main(String []args)not found.
   The main method is static so whenever JVM finds the referring static field or static method or creation of new object,whichever first occurs it will load the class file.

 
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shaveriCommented:
good........
0
g_senthilkumarCommented:
Hi,

The main method we can define like this also
  static public void main(String []args){}.
   The access specifiers can interchange but the retrun type,method name and argument list cannot interchange.The main() is the only method the JVM can understand for Java applications.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.