Home Java Example Java Core What is Public static void main

Related Tutorials


 
 

Share on Google+Share on Google+

What is Public static void main

Advertisement
In most of the Programming language, main function is the first function where program start execution. This is the first method from where Java Virtual Machine(JVM) start executing. Following are the variation of main method.

What is Public static void main

In most of the Programming language, main function is the first function where program start execution. This is the first method from where Java Virtual Machine(JVM) start executing. Following are the variation of main method.

public static void main(String[] args) 
public static void main(String... args)
public static void main(String args[]) //this is most classic signature of main method.

Remember the varargs version of java will work in java 1.5 or later version only.

Main is the entry point of  the class. In java everything is written  within the class so, when you run the java program on the command prompt , loader will load the class and jvm will search for the main method . so making main as static will allow jvm to access without creating object. Main method will strictly follow the rules otherwise your program will not run.

Signature of main method in java:

Public:  main method is declared public because it is visible to other object of other type. It must be public otherwise it not be possible to call it.

Static: main method is static because jvm will call it without creating object. If main is not declared as static then jvm should create a instance of main Class, constructor can be overloaded and can have arguments there would not be any certain way for JVM to find main method in Java.

Void : main is declared void, because main method  does not suppose to return any value, void mean not returning anything.

Example: A simple program how to write a program in java using main method.

class Helloworld
 {
  public static void main(String args[])
     {
       System.out.println("Hello world"); 
      }
  }
  

Output: After compiling and executing the program output will be as follows.

Download Source code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 4, 2013

Related Tutorials

Discuss: What is Public static void main  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com