public static void main(String a,String... args){
// some code
}
2)public static void main(String[] args){//JVM will call this method to start
// some code
}
public class Test{
static public void main( String [] args )
{
System.out.println( "In the JVMs static main" );
main( 5, 6, 7 ); //Calling overloaded static main method
Test t = new Test( );
String [] message = { "Subhash", "Loves", "Programming" };
t.main(5);
t.main( 6, message );
}
public static void main( int ... args )
{
System.out.println( "In the static main called by JVM's main" );
for( int val : args )
{
System.out.println( val );
}
}
public void main( int x )
{
System.out.println( "1: In the overloaded non-static main with int with value " + x );
}
public void main( int x, String [] args )
{
System.out.println( "2: In the overloaded non-static main with int with value " + x );
for ( String val : args )
{
System.out.println( val );
}
}
}
产出:
$ java Test
In the JVMs static main
In the static main called by JVM's main
5
6
7
1: In the overloaded non-static main with int with value 5
2: In the overloaded non-static main with int with value 6
Subhash
Loves
Programming
$
在上面的代码中,出于演示的目的,静态方法和主方法的非静态版本都被重载。注意,通过编写 JVM main,我的意思是说,它是 JVM 首先用来执行程序的 main 方法。
是的,您可以重载 main 方法,但是解释器将始终搜索正确的 main 方法语法以开始执行。.
是的,你必须在 object 的帮助下调用重载的 main 方法。
class Sample{
public void main(int a,int b){
System.out.println("The value of a is " +a);
}
public static void main(String args[]){
System.out.println("We r in main method");
Sample obj=new Sample();
obj.main(5,4);
main(3);
}
public static void main(int c){
System.out.println("The value of c is" +c);
}
}
The output of the program is:
We r in main method
The value of a is 5
The value of c is 3
Class A{
public static void main(String[] args)
{
System.out.println("This is the main function ");
A object= new A();
object.main("Hi this is overloaded function");//Calling the main function
}
public static void main(String argu) //duplicate main function
{
System.out.println("main(String argu)");
}
}
class main_overload {
public static void main(int a) {
System.out.println(a);
}
public static void main(String args[]) {
System.out.println("That's My Main Function");
main(100);
}
}