In Java, JVM (Java Virtual Machine) will always look for a specific method signature to start running an application, and that would be the public static void main (String args[]). The main() method represents the entry point of Java programs, and knowing how to use it correctly is very important.
Syntax
class JBT {
public static void main(String args[]) {
System.out.println("Hello JBT");
}
}
In the above application example, we are using the public static void main. Each word has a different meaning and purpose. Keep learning to know more about each and every keyword.
Public
It is an Access Modifier, which defines who can access this Method. Public means that this Method will be accessible by any Class(If other Classes can access this Class.).
Static
Static is a keyword that identifies the class-related thing. It means the given Method or variable is not instance-related but Class related. It can be accessed without creating the instance of a Class.
Void
It is used to define the Return Type of the Method. It defines what the method can return. Void means the Method will not return any value.
main
Main is the name of the Method. This Method name is searched by JVM as a starting point for an application with a particular signature only.
String args[] / String… args
Here args is an argument of the type String array. Which is used to pass the command-line argument to the main method. Though the type of the argument (String array) is fixed, you can still change the name from args to anything.
Also with the introduction of java args, instead of writing String args[], String… args can be used. You can either use String array (String args[]) or var args(String… args) variable of String type. Both will work the same way.
Given all the options, the syntaxes of the main methods are the same and all will work fine.
public static void main(String[] args)
public static void main(String args[])
public static void main(String jbt[])
public static void main(String... jbt)
Hi, Good explanation!
If I have a Main class (Main.java) and two more classes, Schoolmember and Staff (Schoolmember.java and Staff.java), do I have to have “public static void main(String[] args)” in all of these or only in Main?
Thank You
Leslie
Sorry for the late reply. It depends on what you want to achieve. Main is supposed to work as starting point of the application.
If you are using other classes in Main.java and trying to invoke those methods from Main.java then one main method in Main.java is enough.
But if these classes are independent and you want to execute them individually then they need to have main method too.
i found it nice and easy to understand the topic of the public static void main . It would be better to explain it using video . Than it gates to our mind and if we want to tack it out also it will not come .
Thank you, Kaushal. Creating a video for this short topic is not easy but will see if it is possible.
Well explained. Thank you
hi
public is not access modifier , it is access specifier in java
What does it mean?
Thanks a lot. Explained in an easy way!!
Thanks
Hi, I already knew the main method is the entry point of the program and makes it execute the program properly but this explanation is really good for adults and kids. Thank you very much. This helped me a lot for public static void main(String[] args). I appreciated it a lot. I am a seven year old kid and I love programming.
Welcome Sachet
you are wrong in main definition like “ss” there we are use “is”
Corrected
Static
Static ss a keyword…..
what is the ss means
That was a typo. I meant to say “Static is a keyword….”. It has been corrected now.
A Java program is a sequence of Java instructions that are executed in a certain order. Since the Java instructions are executed in a certain order, a Java program has a start and an end.
Nice explanation….
What is the meaning of creating an instance of the class ??
What is the meaning of creating an instance of the class ??
Please reply me.
Nice One Explanation !!
excellent
good
thank u for the given content\
Main method is very important in java. Program execution starts from main () method only.
why we are using string[] args in pubic static void main(String[] args) why we are not using character array or int array i need elabrate answer
Sir how to call a method how the whole process goes in a program please explain with example
hello sir if we not use static in with voidmain() how we run void main() function
without static keyword
You can write main method without static keyword, there will not be any issue. But it will not work as expected. It will be treated as normal method and in order to execute it you need to call given method explicitly. While static keyword will make this method to be treated as entry point. Hope it clarifies.
thank u for update ur information
i have some cofusion in null point exception
please send me a link of ur site in null point exception
I dont have any article specifically written on null pointer exception
Can you please explain…how internally works these SYSOUT statement??
System.out.println(“args[” +i+ “]:”+args[i] );
System.out.println(“args[ i ]:”+args[i] );
http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/09/command-args.html
1) System -is a class in the java.lang package .
2) out -is a static member of the System class, and is an instance of java.io.PrintStream .
3) println -is a method of java.io.PrintStream. This method is overloaded to print message to output destination, which is typically a console or file.
what is command line argument
Command passed at time of runtime..
On console you pass your input as an argument..
what is array of string
It is a argument passed to Main method.
Regards
why we pass only string as the parameter of the main method?
Argument passed in main method is array of string and not string. This is the standard followed.
I want list and description of in build classes and their sub classes…like printsteam is class and println() is its method…
Why we usethis method main(string args())
It works as starting point of application execution.
Thanks
I guess this will useful for me
hi can you explain why public static void should write in the same order/sequence as written now.
Hi Vinod,
Public and static keyword position can be changed. There is no such rule of using public before static. But as void is method return type it should come just before method name which is main in this case. Hence this sequence.
public static void main
static public void main.
Both are same. But as a standard we right as in the first case.
The grammar in this article and in the comments is horrendous.
u write a new english dictionary
It is shortcut English grammar that is enough for developers to understand each other so they can write efficient English with minimal words – like good programming style that is less verbose
Hi,
I want to know why JVM treats main(String [] args) method as a starting point of program, can anyone tell me the internal functioning of this method, like how it gets execute and what threads are required to execute this method so that JVM can understand this method as the starting point of the program.?
Suppose you are watching a movie and the scene of movie starts from middle of a story and as you continue watching movie, u need to understand the current scene of movie and for that movie makes gives u a starting part of movie in somewhere middle of movie(Gives you information something like” 15years before ” in movie)..by reading this words in movie you get to know the starting of movie…
So likewise u place you main method anywhere in program(inside class) the JVM will read full code in its language to find main() method and starts acknowledging(executing) the code from main method…
and if you dont provide main() method in code the JVM will not understand the story of your code will give error…
good explanation but give me parameters at the same time explanation. i mean which type of parameters in using java.
and please give me different meanings Hello and hello in java
My program still runs if I use other word instead of args in “String[] args”(Parameter to main method). I want to know the reason behind it.
Hi Divya,
args is just a name of argument. You can use any name you want but type should be array of string,
Hope it clarifies.
Regards
Very Good Explanation Thank you
great article.thnk uu
Thank you so much! that’s a good article. I appreciated you
i want more explanation. i know this information.
read java book. java complete reference
Good Article
thanks
It’s really nice & understandable words.
Why “String[] ags” is passed as parameter of main function and without passing this parameter what will happen?
HI,
This is a standard which needs to follow if we want jvm to search it to execute it. There is no harm in removing this parameter from method but then that method will be treated as other java methods and it will not be directly called by JVM.
you will get a compilation error if you dont use that
The main method will compile successfully but will not run. As it will be treated as any other method.
Certain standard to be followed for main method
good to know!!!
hi!!!
Nice Explanation
Thanx
String args[] : Parameter to main method…
Please site how to use the main() with values given to parameters (like what the difference would be with (or without) these values).
If u dont use it shows error in execution….that y we r passing infinite arguments in String type…string is one.type of class ….so,first letter is capital
Hello Sir ,
Nice Info wanted to ask what are the parameters does main method takes
thanku
String args[]
Please fix your grammatical errors. It makes understanding this content difficult.
content is gud…dont alwayz look at grammar
u go for a tuition to learn grammar and become an English professor. you are learning java or checking english errors!
right ,we have developer ,most engineering pass out in first but those student not able to do coding this is different between software developer and english gentleman.
this is very nice info,thank you so much for author.
In this platform not a english debata,it is a programming language platform so does not matter english grammer.(some of the english grammer mistake so we will be adjust……)
You’re a bloody ass, Gaurang.
Hi,
Good Explanation…
Thanking you….