Main() Method of Java

Advertisements

Structure of Java Program Command Line Arguments in Java

Main() Method in Java

main() method is starting execution block of a java program or any java program start their execution from main method. If any class contain main() method known as main class.

Syntax of main() method:

Syntax

public static void main(String args[])
{
.......
.......
} 
main method in java

Public

public is a keyword in a java language whenever if it is preceded by main() method the scope is available anywhere in the java environment that means main() method can be executed from anywhere. 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 method 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 main() method that will be 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.

In case of main() method following changes are acceptable

1. We can declare String[] in any valid form.

  • String[] args
  • String args[]
  • String []args

2. Instance of String[] we can take var-arg String parameter is String...

Syntax

main(String[] args) --> main(String... args)

3. We can change the order of modifiers i.e Instead of

Syntax

public static  we can take  static public

4. Instead of args we can take any valid java identifier.

Syntax

public static  void main(String a[])

We can overload main() method ?

Yes, We can overload 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 modification to this signature, compilation will be successful. But, not run the java program. we will get the run time error as main method not found.

Example of override main() method

Example

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()

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.


Structure of Java Program Command Line Arguments in Java

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements