why is main() static

why do we have to declare main() method of java as 'public' and 'static'?
Who is Participating?
g_senthilkumarConnect With a Mentor Commented:
   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.

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

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

Hope it is clear...

 informative  explaination
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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"); }
for more information visit the  foll..site

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.
All Courses

From novice to tech pro — start learning today.