what's the point of java constructor?

后端 未结 7 1339
野趣味
野趣味 2021-01-07 15:24

So I\'m learning java. I\'m one month in and I just learned about constructors. But I don\'t see the whole purpose of creating one. Why and when would I ever want to use one

7条回答
  •  旧时难觅i
    2021-01-07 15:36

    It is used to create objects. Objects are the main concept in OOP, so creating them is an important step. The main class method is just an entry point to your program. If you don't create objects your program will be procedural rather than object-oriented. This is why to use a constructor.

    And why to create a constructor - sometimes you need to construct an object with some required parameters. There is also a default no-argument constructor, but if you want to initialize your object with additional arguments - you create a constructor taking those arguments.

提交回复
热议问题