为什么主方法是静态的

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.

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.

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 -

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.

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!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值