为什么主方法是静态的

Why main Method is static in Java
When we start learning java and write our first “Hello World” program, there are two things that stand out.

File name and class name should be same in Java.
Main method signature- The main method signature must be public static void main(String[] args)
Here we’ll talk about the main method and the necessity to mark main method as static in Java. Coming to main method signature it is easy to see that main method is-

Public- Access modifier is public so that main method is visible to every other class, same package or other. If it is not public JVM classes will not be able to access it.
Void- As it does not return any value.
String[] args- arguments to this method. The main method takes an array of Strings as parameter, that array is called ‘args’.
But why main method is static in Java is a question that needs some explanation.

In the post Why file name and class name should be same in Java it has already beed discussed that at run time class name should be same, as that’s how JVM knows which class to load and where is the entry point (main method). When you start execution of a Java program, JVM looks for the main method in the provided Java class as that is the entry point for the execution.

But the question here is, how will JVM access that main method with out creating an instance of the class, answer is having main method as static.

为什么主方法是静态的Java当我们开始学习java并编写我们的第一个“Hello World”程序时,有两件事非常突出。在Java中文件名和类名应该相同。Main方法签名——Main方法签名必须是public static void Main (String[] args)这里我们将讨论Main方法以及在Java中将Main方法标记为static的必要性。说到主方法签名,很容易看出主方法是Public- Access修饰符是Public的,这样main方法对其他类、相同的包或其他类都是可见的。如果它不是公共的,JVM类将不能访问它。Void—因为它不返回任何值args-此方法的参数main方法接受一个字符串数组作为参数,该数组称为'args'。但是为什么Java中的main方法是静态的,这是一个需要解释的问题。在为什么在Java中文件名和类名应该相同的帖子中,已经讨论过在运行时类名应该相同,因为这是JVM如何知道加载哪个类和入口点(主方法)在哪里。当您开始执行Java程序时,JVM会在提供的Java类中查找主方法,因为它是执行的入口点。但这里的问题是,JVM如何在不创建类实例的情况下访问主方法,答案是将主方法作为静态的。

Here note that any static method in Java is associated with the class not with any object of the class. Static method can be called without creating any object of the class.

What if main method is not declared as static
If Java main method is not declared as static then instance of the class has to be created which may cause ambiguity, consider the example -

这里需要注意的是,Java中的任何静态方法都与类相关联,而不是与类的任何对象相关联。可以在不创建类的任何对象的情况下调用静态方法。如果主方法没有声明为静态的怎么办如果Java main方法没有声明为静态的,则必须创建类的实例,这可能会引起歧义,考虑这个例子 

public class A {
 private int i;
  A(int i){
  this.i = i;
 }
 public static void main(String args[]){
  A a = new A(5);
 }
}


Here in the class there is a constructor with one argument i. Assume that the main method is not static, which means JVM has to directly create the instance of class A to execute the main method. But in order to create an object of the class constructor has to be invoked which brings up the question what should be passed as i?

JVM wouldn’t know with what values your object has to be initialized. So you have a catch22 situation here in order to get an object of the class this line A a = new A(5); has to be executed (Which is with in the main method) and to execute this line instance of the class is needed if there is no static method in the class.

To avoid these types of ambiguities it doesn’t make sense for the JVM to have to create an object of the class before the entry point (main method) is called. That’s why main method is static in Java.

在这个类中有一个构造函数,它有一个参数i。假设main方法不是静态的,这意味着JVM必须直接创建类a的实例来执行main方法。但是,为了创建类的对象,必须调用构造函数,这就带来了一个问题,应该传递什么作为i?JVM不知道对象必须初始化什么值。所以你有一个两难的情况为了得到类的一个对象这一行a a = new a (5);如果类中没有静态方法,则需要类的实例来执行这一行。为了避免这些类型的歧义,JVM必须在调用入口点(主方法)之前创建类的对象是没有意义的。这就是为什么在Java中main方法是静态的。

Points to note-

main method in Java must be declared public, static and void if any of these are missing; java program will compile but at run time error will be thrown.
At run time interpreter is given the class file which has main method, thus main method is entry point for any java program.
Declaring main method as static in Java ensures that JVM can invoke the entry point (main method) with out creating any instance of the class.
With varargs in Java 5 onward it is possible to write the main method as public static void main(String … args).
That’s all for this topic Why main Method is static in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!
点需要注意Java中的main方法必须声明为public、static和void(如果其中任何一个缺失);Java程序将被编译,但在运行时将抛出错误。在解释器运行时,给出了具有main方法的类文件,因此main方法是任何java程序的入口。在Java中将主方法声明为静态方法可以确保JVM在不创建类的任何实例的情况下调用入口点(主方法)。使用Java 5之后的varargs,可以将main方法编写为public static voidmain(String ....参数)。这就是为什么main Method在Java中是静态的。如果您有任何疑问或建议,请发表评论。谢谢!

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kjshuan

点个赞就好啦!!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值