Main method
In order to write a program in java, we need to follow 3 standard steps
- Write a program
- Validate/compile
- Execute
Example:
1. Write a program:
class My_Program { // access specifier is optional, class keyword, class name, the start of a class
----------------------- // statements
------------------------ // ststements
} // end of a class
2. Compile the program:
Now compile the program
>>> javac My_Program.java
If there are no errors in the program .class file will generate.
The moment we click on
enter here JVM is going to start. Once JVM is started in this desktop JVM check whether this .class file is there or not.
JVM loads the .class file, whenever JVM is loading the .class file automatically all the static variables and static methods are going to be created.
3. Execute the program:
JVM will start the execution
>>> java
Whenever you ask the JVM to
run the program JVM is going to look for the starting point to execute the
program
Ex: To start a car we need a key point.
In the same way to execute the java program we need a starting point.
- JVM looks for .class file
- Loads the .class file
- JVM looks for execution point
What is the execution
point?
JVM is always going to start
the execution from the main() method.
The main method is the
starting point for the execution
If the main method is not
there we will get an error.
What is the main method
in java?
In java, the main method
is there for two reasons
1. Whenever you ask the JVM
to run the program, JVM always starts execution from the main method.
why?
Because JVM is internally
configured in such a way that whenever the programmer is asked to run the
program always starts the execution from the main method.
If the main method is not
there JVM doesn't know where to start the execution that's why it's throwing a
compile-time error.
* In order to execute a
program, we need a main method
2. Inside the main method
what we will do?
We will do testing
How to define the main
method?
Standard syntax:
<access specifier> <static keyword> <void> <main> <(String[] args)>
{ // start of the main method
---------------------
--------------------
} // end of the main method
String ---> Here S should be capital letter as per the java rules
There are several ways to define main method in java program:
1. public static void
main(String[] args)
{
// accessing or testing
the implemented services or methods
}
2. static public void
main(String[] args)
{
// accessing or testing
the implemented services or methods
}
3. public static void
main(String... args)
{
// accessing or testing
the implemented services or methods
}
In place of the args
whatever we want we can write
4. public static void
main(String[] CREATIVEKODZ)
{
// accessing or testing
the implemented services or methods
}
5. public static void
main(String... CREATIVEKODZ)
{
// accessing or testing
the implemented services or methods
}
/* In place of [] we can
write ... three dots
6. strictfp public static
void main(String[] args)
{
// accessing or testing
the implemented services or methods
}
All the above main
methods are valid but commonly we use first method
Main method is by default
static method.
Where we need to define
the main method in a program?
1. After starting the class immediately take main method
public class My_Program {
public static void
main(String[] args)
{
// accessing or testing
the implemented services or methods
}
;;;;;;;;;;;;;;;;;;;///defined
or implemented part
;;;;;;;;;;;;;;;;;
}
2. Before end of a class
take main method
public class My_Program {
;;;;;;;;;;;;;;;;;;;///defined
or implemented part
;;;;;;;;;;;;;;;;;
public static void
main(String[] args)
{
// accessing or testing
the implemented services or methods
}
}
3. In between the class
take main method
public class My_Program {
;;;;;;;;;;;;;;;;;;;///defined
or implemented part
;;;;;;;;;;;;;;;;;
public static void
main(String[] args)
{
// accessing or testing
the implemented services or methods
}
;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;
//implemented or defined part
}
4. Take main method in
seperate class
<as>
class <classname> {
;;;;;;;;;;;;;;;;;;;///defined
or implemented part/services part
;;;;;;;;;;;;;;;;;
}
<as>
class <classname> {
}
<as>
class <classname> {
public static void
main(String [] CREATIVEKODZ)
{
// accessing or testing
the implemented methods / services
// execution part
}
}
If we remove static keyword we will get a compile-time error.
public static void
main(String[] CREATIVEKODZ) ---> valid
public void main(String[] CREATIVEKODZ) ---> invalid
public static void
main(String[] ...) ---> valid
If we want to execute the
program in java compulsory JVM always starts the execution from the main
method.
Whenever the programmer asks the JVM to run the program JVM always looks for main method.
Main method is the starting point
for the program.
If we don't define main method
immediately we will get compile time error.
If there is no main method we cannot execute the program.
The basic java program is:
public class My_Program { // start of a class
public static void main (String[] args) { //start of main method
-------------------- // statements
-------------------- // statements
} //end of main method
} // end of a class
check your knowledge
I hope you have learned something new today!
Now it's time to check your knowledge!!
Let's answer the below queries
Write answers to the following questions in the comments section
1. What is main method?
2. Is static keyword is mandatory?
3. Write any three syntaxes of main method
4. How many standard steps are there in java to write a program and what are they?
5. What is execution point?
6. From where JVM starts the execution of a program?
7. Write a java basic program with class definition and main method?
8. Where we can define main method in a program?