In this post i will tell you something interesting about main() method in java. Like
What is the use of main() method?
How we are going to use main() method?
Why we are using main() method in our java program?
You will find the answers for all these questions in this post only.
What is the use of main() method in java ?
Let me show you an example showing the above mentioned exception:-
save it as FirstProgram.java and run the code you will get a run-time exception like this,
But in java if you define any static block then priority will be given to that block then to the main() method.
let me make you more clear regrading the above statement with the help of an example.
Here in the above program we have a static block and a main() method. If you see the output of this program it is a bit interesting.
Output -- This a static block
This is a main() method
Here you can see static block is executed first but why?
If we are thinking of executing program having static block but no main() method?
How we are using main() method in java ?
It has got some syntax i.e ,
public static void main(String str[ ]){ }
Here you can see that public and static both are modifiers and there is no arrangement for modifiers declarations in java, we can use like this also,
static public void main(String str[ ]){ }
The above syntax will work fine like the 1st statement.
Why it is static ?
-->It is because just above i showed you that static block will be initialized while loading the .class file. So main() is a static method so your code can run freely without the involvement/creation of any object.
And if you missed out writing public then while executing, it will show a message -
" main method not public " .
Save it as FirstProgram.java then compile and run will give the output as,
Output:- The length of the str variable is : 0
Details about how to pass and print the value just Click Here ! .
What is the use of main() method?
How we are going to use main() method?
Why we are using main() method in our java program?
You will find the answers for all these questions in this post only.
What is the use of main() method in java ?
Basically we can say that it is the starting point for the java program.
Let suppose there is no main() method in your program and you have many definition and declarations in your program then from where the JVM will start executing your java program ?
---> Your JVM will confused about starting point of the java program. So to be not a part of any confusion, java developers sets that main() method would be the starting point of any java program. Still we can execute without main method but JVM will throw run-time exception saying that "java.lang.NoSuchMethodError" . But note that it is not a compilation error but is a run-time exception.
import java.lang.Object;
public class FirstProgram {} // This class is without main()method
save it as FirstProgram.java and run the code you will get a run-time exception like this,
But in java if you define any static block then priority will be given to that block then to the main() method.
let me make you more clear regrading the above statement with the help of an example.
import java.lang.Object;
public class FirstProgram {
static {
System.out.println(" This is static block ");//print statement in static block
}
public static void main(String str[]) {
System.out.println(" This is main() method ");//print statement in main()
}
}
Here in the above program we have a static block and a main() method. If you see the output of this program it is a bit interesting.
Output -- This a static block
This is a main() method
Here you can see static block is executed first but why?
--> Reason is that, static is the scope of class and all the static variables are initiated / loaded at the class loading time that means before executing the program. JVM is responsible for loading the .class file. And the program will be executed after loading the .class file. To know about static Click Here ! and for class loading just Click Here ! .
If we are thinking of executing program having static block but no main() method?
--> Its simple, you can run your program with above mentioned condition but only difference is that it will print the content of static block first then will give run-time exception "java.lang.NoSuchMethodError".
Now you can verify that class loading is done first then execution will start. Means JVM read the static block before executing the program. That is why it is printing the content of static block first and then giving run-time exception about main() method.
For confirmation of above statement try to run the above program without main() method. The output will look like this,
How we are using main() method in java ?
It has got some syntax i.e ,
public static void main(String str[ ]){ }
Here you can see that public and static both are modifiers and there is no arrangement for modifiers declarations in java, we can use like this also,
static public void main(String str[ ]){ }
The above syntax will work fine like the 1st statement.
Why it is static ?
-->It is because just above i showed you that static block will be initialized while loading the .class file. So main() is a static method so your code can run freely without the involvement/creation of any object.
And if you missed out writing public then while executing, it will show a message -
" main method not public " .
void :- void indicates the return type of the main() method, that is , it is not going to return any value.
main(String str[]) :- Arguments with the main() method indicate that you can pass any String value while executing your program through command prompt . Here you can see that it is String type that means you can pass a string value and the variable str[] is a array type, it means that it can accept any number of argument. Passing value at the time of execution of the java program is called as command-line-argument.
Try to print the length of the str[ ] variable. Like this,
import java.lang.Object; // import statement
public class FirstProgram {
public static void main(String str[]) {
System.out.println(" The length of the str variable is :" +str.length);
}
}
Save it as FirstProgram.java then compile and run will give the output as,
Output:- The length of the str variable is : 0
Details about how to pass and print the value just Click Here ! .
EmoticonEmoticon