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

后端 未结 15 1234
借酒劲吻你
借酒劲吻你 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:22

    Constructor is used to create Objects.

    Static is generally which is same for all objects.

    So, if we have had static constructors creation of one object would affect all the other existing objects.

    Static methods only reference to static variables. Therefore all the initial parameters which you are giving to create an object would change for all objects. It is no point creating similar objects for no use.

    Hope this helps.... :)

提交回复
热议问题