Main Method Lyst3867
Main Method Lyst3867
In Java, the "new" keyword is used to create new objects. There are three steps when creating an object
from a class −
Even there are 4 functions or method present in this program will not get executed because it requires
the control of execution to begin.
1
2
Main method in Java
C Language
#include <stdio.h>
void main()
{
printf("HELLO WORLD");
}
Step 1
In Java, basics rule of java is that every single method / function compulsory present within a Class.
3
Control cannot see the main method because we have enclosed by class Demo.
To make the main method visible to the control though it is enclosed within class.
By attaching PUBLIC
Step 2
Control can see the main method because we have declared it has public unfortunately it is not
accessible by the control.
To make the main method visible to the control though it is enclosed within class.
By attaching STATIC
Step 3
4
Will the above program execute now?
Unfortunately, it will not show the output because JVM will start looking for Identifier as a starting
point.
It is the command line argument which stores data and it is nothing but an array.
Step 4
String [] args
What is args?
5
Arguments means data passed on the command line.
Args is technically called as "Dynamic Array" using which the command line arguments are collected.
In java, along with the control of execution we can also give inputs/ data to the main method.
To collect the input / data there is args. Initially args is an empty basket.
6
Key points
● Always save the source file same as the class name in which main() is present along with the
.java extension if it is written in notepad.
● When javac source_file_name.java is run in command prompt then a .class file creates which is
in byte code format.
● To convert the byte code file to machine level all you have to do is run the following line in
command prompt: java source_file_name
● If the main method is not made as public then during execution an error is popped saying main
method not found.
● If the main method is not static then during execution it shows error as main method not static.