Home > Software > Java > Core-Java
Interview Questions   Tutorials   Discussions   Programs   

Core-Java - In Java why we write public static void main(String args[]) Why not main()?

asked marvit August 27, 2014 05:07 AM  

In Java why we write public static void main(String args[]) Why not main()?


1 Answers

answered By Madhumathi   0  
main method is the entry point for any java program . 
Its syntax is :

             public static void main(String[] args) 

1. Since the main method is static , the JVM(Java Virtual Machine) can call the main method without creating an instance of the class which contains the main method. 

If the main method was not declared as static then JVM has to create the instance of the class which contains main . And since the constructors can be overloaded and can have arguments there would not be any consistent way for JVM to find main method in Java.

Anything which is declared in class in Java comes under reference type and requires object to be created before using them but static method and static data are loaded into separate memory inside JVM called context which is created when a class is loaded. If main method is static than it will be loaded in JVM context and are available to execution.

2. Any method or variable which is declared public in Java can be accessible from outside of that class. Since main method is public in Java, JVM can easily access and execute it.

3. void is the return type , which means main doesnt return anything .

   add comment

Your answer

Join with account you already have



Ready to start your tutorial with us? That's great! Send us an email and we will get back to you as soon as possible!