Understanding public static void main in java
public static void main in java |
public static void main in java
Let's split public static void main in java and understand it one by one.
![]() |
What is public static void main in java |
public :
- public is an access modifier, which specifies from where and who can access this method.
- public makes the method available globally.
- public keyword is used before the main method to specify the starting execution point of the program and will be visible to JVM.
Example :
What will happen if we change public to private or protected or default with no access modifier
below is the code snippet for all the three and its output
public class JavaCodeGeekMainExample {
private static void main(String[] args) {
System.out.println("Hello World");
}
}
public class JavaCodeGeekMainExample {
protected static void main(String[] args) {
System.out.println("Hello World");
}
}
public class JavaCodeGeekMainExample {
static void main(String[] args) {
System.out.println("Hello World");
}
}
output:
main method with private access modifier |
As we can see above, its throwing error at runtime in all the three cases :
Main method not found in class JavaCodeGeekMainExample, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Static:
- static is the keyword which defines the method static, static keyword makes method as class related.
- static methods make method available to be invoked by JVM without even creating instance of class.
- No need of object creation is there when we have static property defines as we can call directly using ClassName.property as in this case main method called by JVM directly by ClassName.
What will happen if we remove static from public static void main(String args[]) lets have a look
Example :
public class JavaCodeGeekMainExample {
public void main(String[] args) {
System.out.println("Hello World");
}
}
output:
main method without static keyword |
Error: Main method is not static in class JavaCodeGeekMainExample, please define the main method as :
public static void main(String[] args)
void:
- void is a keyword that is used to specify that method does not return anything.
- As the method completes, the java program terminates.
Lets see now what will happen when we remove void from public static void main(String[] args)
Example:
public class JavaCodeGeekMainExample {
public static main(String[] args) {
System.out.println("Hello World");
}
}
output:
|
In this as we have removed void thus our code is not even compiling and giving us compile time error
JavaCodeGeekMainExample.java:2: error: Invalid method declaration; return type required
public static main(String[] args){
1 error
main:
- main is having the default signature which is recognised by JVM.
- It is the starting point of any java program which JVM looks for.
- we can overload main methods with different parameters or no parameter at all.
- If we change the name of this main method to something else compiler will give us runtime error.
Example:
public class JavaCodeGeekMainExample {
public static void differentMain(String[] args) {
System.out.println("Hello World");
}
}
output:
main method defined with different name |
String[] args
- main method accept data from user at runtime in form of command line arguments in java. This String[] args hold those command line arguments passed at runtime in form of string values
- Name of String[] args is not fixed and can be anything user wants to name it.
Example:
public class JavaCodeGeekMainExample {
public static void main(String[] args) {
System.out.println("Hello " + args[0]);
}
}
output:
main command line arguments |
As shown in image above we have passed World as an command line runtime arguments and when programs runs successfully output comes as Hello World.
If we don't pass any argument then it will give us runtime exception as our program expects one single argument to be passed at runtime below is the error shown
main indexoutofboundexception when args not passed |
As shown in the above image it gives us
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at JavaCodeGeekMainExample.main(JavaCodeGeekMainExample.Java:3)
There are different ways to write String[] args in main method
As String args[] as compared to String[] args
public static void main(String args[])
Providing varargs as parameter to main method like String... args
public static void main(String... args)
Different ways to writing main method
1. Changing public and static positions.
static public void main(String args[])
2. Defining different name for String[] args to any other name.
public static void main(String arguments[])
3. Using Varargs as parameter to main method.
public static void main(String... args)
Conclusion:
In this tutorial, we have covered public static void main in java which is the initial and first most program that every beginner learn.
Thanks for reading this tutorial so far. If you like this tutorial then please share it with your friends and colleagues. If you have any questions, doubts, suggestions, or feedback then please drop a comment and I'll try to answer your question.
So what do you think about this tutorial? It's Easy Right?
Happy Learning!!!
More Examples you may like
Comments
Post a Comment
If you have any doubts, Please let me know.