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

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

    Java does not permit to declare a constructor as static. Following are the reasons.

    1. Static means for the same class. i.e, static methods cannot be inherited.

    2. With static, "this" reference (keyword) cannot be used. "this" is always linked to an object. A constructor always belongs to some object.

    3. If a constructor is static, an object of subclass cannot access. If static is allowed with constructor, it is accessible within the class but not by subclass.

提交回复
热议问题