What is main Method in java ? how to use main method in Java ? can we overload main method ? why main method is static?

What is the main Method in java? how to use the main method in Java ? can we overload the main method? why the main method is static

 What is the main Method in java?

The main method is the entry point that means it is the starting of every java program. if JVM runs any program for java it first needs is find the main method in java.

it is always written as public static void main(String[] args).

  • public: Public is an access modifier, which is used to define who can access this method that means who can use this method. Public means that this Method will be accessible by any Class.
  • static: Static keyword used for memory management. It is a keyword in java that identifies it is class-based. main() is made static in Java so that it can be accessed without creating the instance of a Class. In case, main is not made static then the compiler will throw an error as main() is called by the JVM before any objects are made and only static methods can be directly invoked via the class. 
  • void: It is the return type of the method. Void defines the method which will not return any value.
  • main: It is the name of the method which is searched by JVM as a starting point for an application with a particular signature only. It is the method where the main execution occurs.
  • String args[]: It is the parameter passed to the main method.

how to use main method in Java ?

the following example define the how we use main method in java.

public class Fibonic {

       public static void main(String args[]) {

              // 0, 1, 1, 2, 3, 5, 8

              int t1 = 0;

              int t2 = 1;

              for (int i = 0; i <= 10; i++) {

                     System.out.print(t1 + " + ");

                     int sum = t1 + t2;

                     t1 = t2;

                     t2 = sum;

              }

       }

}

the above program shows the fibonic series.

can we overload the main method?

yes, we can overload the main method in the same class. first, we need to know that what is overloading. overloading is a technique or mechanism that provide the facility of making same method in your current class by changing the type of argument , no argument , an order of argument. this is just one way, you can create as many main version of main as you want. in simple words, overloading can define as a feature in which class can have more then one method having the same name if and only if they differ by a number of parameters or the type of parameter or both.

Yes, We can overload the main method in java but JVM only calls the original main method, it will never call our overloaded main method.
The below example illustrates the overloading of main() in java.

//Java program to demonstrate

//Overloading of main()

public class test {

       // Overloaded main method 1

       // According to us this overloaded method

       // Should be executed when int value is passed

       public static void main(int args) {

              System.out.println("main() overloaded" + " method 1 Executing");

       }

       // Overloaded main method 2

       // According to us this overloaded method

       // Should be executed when character is passed

       public static void main(char args) {

              System.out.println("main() overloaded" + " method 2 Executing");

       }

       // Overloaded main method 3

       // According to us this overloaded method

       // Should be executed when double value is passed

       public static void main(Double[] args) {

              System.out.println("main() overloaded" + " method 3 Executing");

       }

      // Original main()

       public static void main(String[] args) {

              System.out.println("Original main()" + " Executing");

       }

}

 Why the main method is static?

first thing in your mind when you hear the static words. that means the static method belongs to class not to object. you can access static method or variable without the creation of class objects because they belong to class not to object. so as per execution of our program java compiler can call the main method without the creation of an object or before the creation of the object of the class. In any java program, the main() method is a starting point from where compiler starts program execution.


Comments

Popular posts from this blog

What is SIP or Session Initiation Protocol ?

How To Create An Object In Java ?

How do I declare and initialize an array in Java?