Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Core Java

  Asked By: Jidhin    Date: Jun 11    Category: Java    Views: 762
  

Why public static void main is used always, why not "private" ?

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Archana Kamble     Answered On: Sep 05

The execution of the program begins from the main method. The main method is static so it is invoked by the virtual machine using the class instance internally.
For eg if your class name is "Basic.java" which has public static void main(String args[]) method.
Internally it will call Basic.main();

If it was instance method the VM would have to create Object first then call the main method .This is wastage of memory .Everything main is called it will have to create object then call main method.

main() is public because it is the starting point that is used by the VM to start the execution of the program.

If it is private it won't be visible to the VM.

 
Didn't find what you were looking for? Find more on Core Java Or get search suggestion and latest updates.




Tagged: