main() method is starting execution block of a java program or any java program start their execution from the main method. If any class contains the main() method known as the main class.
Syntax : public static void main( String args[] )
{
...................
...................
}
Public
public is a keyword in a java language whenever if it is preceded by a main() method the scope is available anywhere in the java environment that means the main() method can be executed from anywhere. the main() method must be accessed by every java programmer and hence whose access specifier must be public.
Static
static is a keyword in java if it is preceded by any class properties for that memory is allocated only once in the program. Static methods are executed only once in the program. main() method of java executes only once throughout the java program execution and hence it declare must be static.
Void
void is a special datatype also known as no return type, whenever it is preceded by a main() method that will never return any value to the operating system. main() method of java is not returning any value and hence its return type must be void.
String args[]
String args[] is a String array used to hold command line arguments in the form of String values.
We can overload the main() method?
Yes, We can overload the main() method. A Java class can have any number of main() methods. But run the java program, which class should have main() method with signature as public static void main(String[] args) If you do any modifications to this signature, the compilation will be successful. But, not run the java program. we will get the run time error as the main method is not found.
Why main Method is Declared Static ?
Because object is not required to call static method if main() is non-static method, then JVM create object first then call main() method due to that face the problem of extra memory allocation.
Program:
public class mainclass { public static void main(String[] args) { System.out.println("Execution starts from Main()"); } void main(int args) { System.out.println("Override main()"); } double main(int i, double d) { System.out.println("Override main()"); return d; } }Output:
Execution starts from Main()- Progamming is what actually means ?
- What is software development ?
- How we categorized software development in different manner.
C Plus Plus
Python
Java Development
Web Designing
Javascript
Mysql
Oracle