Why can other methods be “static” but a constructor cannot?

后端 未结 15 1193
借酒劲吻你
借酒劲吻你 2020-12-04 17:59

I do not understand why the main method has to be static. I understand static variables but static methods are difficult for me to grasp. Do static method exists so that one

15条回答
  •  無奈伤痛
    2020-12-04 18:43

    I wrote a simple example as an answer to a related question yesterday which may help make things more understandable: what's the point of java constructor?

    The point of Static methods is that they can be called without creating an instance of a class, while "normal" instance methods are related to an instance, and can not be called without one.

    Since the Main method of the Main class is the entry point of the program, no instance can possibly have been created yet, so naturally, you can not access it via an instance. Therefore, it is Static, so it can be run as the start of the program.

提交回复
热议问题