我们可以在Java有多个主要方法吗?
Java是一种面向对象的语言,所有处理都在类中进行。程序的执行意味着让Java虚拟机加载类,然后开始执行它的main方法。 Java 的 main 方法是任何Java程序的入口点。在 main 方法之前使用公共访问修饰符,以便 JVM 可以识别程序的执行点。如果使用其他访问修饰符而不是 public,它将对 JVM 不可见。
最初,JVM 加载该类,但不存在该类的对象来调用 main 方法。这就是为什么 main 方法必须是静态的,以便 JVM 可以加载类并在没有类对象的情况下调用 main 方法。 Java main 方法不返回任何内容,这就是其返回类型为void 的原因。如果我们尝试从 main 方法返回任何东西,它会给出 意外的值错误 因为它是 JVM 中的预定义签名。 Java 的 main 方法接受字符串数组作为参数。它也称为命令行参数,它可以在 main 方法中从命令行传递。现在让我们通过附加干净的Java程序来实现相同的功能。
执行:
示例 1
Java
// Java Program Illustrating Can we have Multiple main
// methods
// Importing input output classes
import java.io.*;
// Main class
class GFG {
// Method 1
// Method inside main() method
void test()
{
// Print statement whenever this method is called
System.out.print("Inside class GFG");
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Creating an object class inside main() method
GFG obj = new GFG();
// Calling the class object inside main() method
obj.test();
}
}
Java
// Java Program Illustrating Can we have
// Multiple main() Methods
// Importing input output classes
import java.io.*;
// Main class
class GFG {
// Method 1
void test()
{
// Print statement when this method is called
System.out.print("inside test");
}
// Method 2
// Main driver method
public static void main(int i)
{
// Creating object later calling of class
// inside this main() method
GFG obj = new GFG();
obj.test();
}
// Method 3
// Main driver method
public static void main()
{
// Creating object later calling of class
// inside this main() method
GFG obj = new GFG();
obj.test();
}
}
Java
// Java Program Illustrating Can we have
// Multiple main() Methods
// Importing input output classes
import java.io.*;
// Main class
class GFG {
// Method 1
// Main driver method
public static void main(int i)
{
// Print statement for method 1
System.out.println(i);
}
// Method 2
// Main driver method
public static void main(String s)
{
// Print statement for method 2
System.out.println(s);
}
// Method 3
// Main driver method
public static void main(String[] args)
{
// Calling above 2 main methods
main(1);
main("hi");
}
}
Inside class GFG
In the above program we are simply calling test() method by using class object from main() method. Now let us go onto depicting program having multiple main() methods.
示例 2
Java
// Java Program Illustrating Can we have
// Multiple main() Methods
// Importing input output classes
import java.io.*;
// Main class
class GFG {
// Method 1
void test()
{
// Print statement when this method is called
System.out.print("inside test");
}
// Method 2
// Main driver method
public static void main(int i)
{
// Creating object later calling of class
// inside this main() method
GFG obj = new GFG();
obj.test();
}
// Method 3
// Main driver method
public static void main()
{
// Creating object later calling of class
// inside this main() method
GFG obj = new GFG();
obj.test();
}
}
输出:
输出说明:
上面的程序由两个main方法组成,但是抛出类中找不到Main方法的错误,请定义main方法 方法为 public static void main(String[] args)” 。只有以单个字符串数组作为参数的 main() 方法才被视为程序的入口点。 JVM 只查找带有字符串数组作为参数的 main 方法。为了执行其他主要方法,您需要从内部调用它们 public static void main(String[ ] args)
示例 3
Java
// Java Program Illustrating Can we have
// Multiple main() Methods
// Importing input output classes
import java.io.*;
// Main class
class GFG {
// Method 1
// Main driver method
public static void main(int i)
{
// Print statement for method 1
System.out.println(i);
}
// Method 2
// Main driver method
public static void main(String s)
{
// Print statement for method 2
System.out.println(s);
}
// Method 3
// Main driver method
public static void main(String[] args)
{
// Calling above 2 main methods
main(1);
main("hi");
}
}
1
hi
From the above program, we can say that Java can have multiple main methods but with the concept of overloading. There should be only one main method with parameter as string[ ] arg.