Can we say Constructors are Methods in Java?

后端 未结 4 465
礼貌的吻别
礼貌的吻别 2021-01-29 15:57

As we know Java is an object oriented language. Everything is objects in Java. We also know that objects have something (instance variables / fields) and do something (methods).

4条回答
  •  面向向阳花
    2021-01-29 16:27

    Directly from the JLS (Chapter 8)

    A constructor is used in the creation of an object that is an instance of a class (§12.5, §15.9). The SimpleTypeName in the ConstructorDeclarator must be the simple name of the class that contains the constructor declaration; otherwise a compile-time error occurs.

    In all other respects, the constructor declaration looks just like a method declaration that has no result (§8.4.5).

    Constructor declarations are not members. They are never inherited and therefore are not subject to hiding or overriding.

提交回复
热议问题