Here we will learn different ways in which we can write the main method in JAVA. Different ways of modifying the main method in JAVA are mentioned below and displayed with the help of example:
1) Making changes in the sequence of modifiers, method prototype.
1.a) General method of writing main method in java. Sample code.
class G {
public static void main(String arg[]){
System.out.println("This is the general way of writing main method");
}
}
Output:
This is the general way of writing main method
1.b) static public void main(String args[])
class F {
static public void main(String arg[]){
System.out.println("Using 'static' before 'public' in main method");
}
}
Output:
Using 'static' before 'public' in main method
2) Making changes in java array by using java array after type or before
variable or after variable.
Below are the different ways for writing main method.
2.a) public static void main(String[] args)
class E {
static public void main(String[] arg){
System.out.println("Square brackets are written after String");
}
}
Output:
Square brackets are written after String
2.b) public static void main(String args[])
Sample code.
class D {
static public void main(String arg[]){
System.out.println("Square brackets are written after arg");
}
}
Output:
Square brackets are written after arg
2.c) public static void main(String []args)
Sample code.
class C {
public static void main(String []arg){
System.out.println("Square brackets are written before arg");
}
}
Output:
Square brackets are written before arg
3) By passing 3 dots in the main method with String parameter
3.a) public static void main(String... args)
Sample code is below:
class B {
public static void main(String... arg){
System.out.println("Using three dots with after String i.e. ...");
}
}
Output:
Using three dots with after String i.e. String...
0 Comment(s)