Valid Variants of main() in Java

In the given tutorial, we will learn about valid variants of main() method in Java.

First, we should see the meaning of the main syntax :

  • public : This is the access specifier. Because of this, JVM can execute the main() method from anywhere.
  • static : This is a keyword. Because of this, main() is called without creating an object.
  • void : This represents the return type of main() method. It shows that main method doesn’t returns anything.
  • main : This is an identifier. It represents method name.
  • String[] args : This is a Array of String type. Because it accepts the command line arguments.

Standard or most general way of declaring main() is :

class Demo
{
 public static void main(String[] args)
 {
  System.out.println("Welcome to CodeSpeedy...!!!");
 }
}

We will get the output given below for the above code:

Welcome to CodeSpeedy...!!!

 

We can make some adequate changes in the above syntax of main() which are valid  :

1) We can change the Order of the Modifiers :

Interchanging the positions of static and public in main() is valid. Also, we can interchange the positions of [] and args ( Array representation) positions.

class Demo
{
 static public void main(String[] args)
 {
  System.out.println("Welcome to CodeSpeedy...!!!");
 }
}

And the output result will be:

Welcome to CodeSpeedy...!!!

 

2) Use of varargs :

We can replace square brackets by varargs () for string parameter.

class Demo
{
 public static void main(String ...args)
 {
  System.out.println("Welcome to CodeSpeedy...!!!");
 }
}

The above program will give this output:

Welcome to CodeSpeedy...!!!

 

3) Making the String array argument as Final :

class Demo
{
 public satic void main(final String[] args)
 {
  System.out.println("Welcome to CodeSpeedy...!!!");
 }
}

The output result for the above code be:

Welcome to CodeSpeedy...!!!

 

4) main() can be declared as Final :

class Demo
{
 public final static void main(String[] args)
 {
  System.out.println("Welcome to CodeSpeedy...!!!");
 }
}

The output of the given program is :

Welcome to CodeSpeedy...!!!

 

5) main() can be declared as synchronized :

class Demo
{
 public synchronized static void main(String[] args)
 {
  System.out.println("Welcome to CodeSpeedy...!!!");
 }
}

The output will be:

Welcome to CodeSpeedy...!!!

 

6) main() can be declared using the keyword strictfp, which is used for restraining of floating point calculations :

class Demo
{
 public strictfp static void main(String[] args)
 {
  System.out.println("Welcome to CodeSpeedy...!!!");
 }
}

The above code will show the output as you can see:

Welcome to CodeSpeedy...!!!

 

7) We can declare main() as final, synchronized and strictfp, all at a time :

class demo
{
 final static synchronized strictfp static void main(String[] args)
 {
  System.out.println("Welcome to CodeSpeedy...!!!");
 }
}

We get the output given below for the above code:

Welcome to CodeSpeedy...!!!

 

8) main() can be overloaded by giving different forms of argument :

class Demo
{
 public static void main(String[] args)
 {
  System.out.println("Main method having Sring array as an argument");
 }
 public static void main(int[] args)
 {
  System.out.println("Main method having int array as an argument");
 }
}

And the output for the above code is:

Main method having String array as an argument

 

9) Inheritance of main method :

main() can be inherited with no errors.

class Demo1
{ 
 public static void main(String[] args)
 {
  System.out.println("Main method of Super class");
 }
}

class Demo2
{

}

After execution of class Demo1 or Demo2 the output of both of this classes will be :

Main method of Super class

10) We can hide main() without overloading it :

main() is a static method and we can’t override the static methods of super class in the sub class. Even if we declare the same method in sub class, it will not override the static method of super class. Instead of that, it will only hide the super class method. This is also known as Method hiding.

class Demo1
{
 public static void main(String[] args)
 {
  System.out.println("Main method of Super class");
 }
}

class Demo2 extends Demo1
{
 public static void main(String[] args)
 {
  System.out.println("Main method of Sub class");
 }
}

After execution of class Demo1 the output will be :

Main method of Super class

After execution of class Demo2 the output will be :

Main method of Sub class

Leave a Reply

Your email address will not be published. Required fields are marked *