Java Main Method - public static void main(String args[])
Java main() is a important method in java programs and the main() method is look like public static void main(String args[]) in java.
Java main() method is the entry point of any java program i.e java main() method is a standard method which is used by JVM to start execution of any java program.
In other words, Java main() is a method and every java programs execution start from the public static void main(String args[]) i.e java main method and this java main method is called by the JVM when we execute our java program.
In java if any class contains main() method i.e public static void main(String args[]) know as main class.
Syntax of main() method :
public static void main(String args[])
{
----
----
}
Java main() method is the entry point of any java program i.e java main() method is a standard method which is used by JVM to start execution of any java program.
In other words, Java main() is a method and every java programs execution start from the public static void main(String args[]) i.e java main method and this java main method is called by the JVM when we execute our java program.
In java if any class contains main() method i.e public static void main(String args[]) know as main class.
Syntax of main() method :
public static void main(String args[])
{
----
----
}
Java Main Method Example
This is a simple example of java main method where we will define java main method as public static void main(String args[]). This is simple "hello world " program in java.
class JavaMainMethod
{
public static void main(String args[])//main method of java
{
System.out.println("hello world");
}
}
output : hello world
Now, let's understand each part of the main method like public , static, void and main(String args[]).
Description give below of public static void main(String args[])
class JavaMainMethod
{
public static void main(String args[])//main method of java
{
System.out.println("hello world");
}
}
output : hello world
Now, let's understand each part of the main method like public , static, void and main(String args[]).
Description give below of public static void main(String args[])
Public
In java public is an access modifier and the scope of public access modifier is available everywhere in java environment.
If we do not make any method as public, then we cannot call that method in any other program. This is the reason that we make main method public so that JVM can call main method from anywhere and execute our java program, So main method should be public.
If we do not make any method as public, then we cannot call that method in any other program. This is the reason that we make main method public so that JVM can call main method from anywhere and execute our java program, So main method should be public.
Static
Static keyword in java which is basically used for memory management i.e to save memory or to make memory efficient. If we make any method as static then there is no need to create an object because we can call that method by using class name and this make our memory efficient because object take a memory.
To execute main method without creating object then the main method should be static so that JVM can call main method by using class name itself and when java runtime starts, there is no objects of class presents. That's why main method should be static so that JVM can call it by class name.
Void
In java void is a return type that doesn't return anything. Java main method is not returning any values and hence its return type must be void.
Main
Java main() is the name of the method where java program starts their execution, It is the entry point of any java programs.
String args[]
In java String args[] is a String array which accepts the command line arguments in the form of String values. String args[] also called command line arguments in java.
Different Ways To Writing Main Method In Java
Case 1 : Changes with public, static
- public static void main(String args[])
- static public void main(String args[])
Case 2 : Changes with main(String args[])
- main(String args[])
- main(String[] args)
- main(String []args)
Case 3 : Changes with public static void main(String args[])
Using 3 dots(...) in main(String... s)
- public static void main(String... s)
Q. Can We Overload Main Method In Java ?
Ans. Yes, We can overload main method in java.
Q. Can We Override Main Method In Java ?
Ans. No, We cannot override main method in java.
Q. Can We Apply Final Keyword With Main Method In Java ?
Ans. Yes, we can apply final keyword with main method e.g
final public static void main(String... s)