Why we use init() rather Constructor

前端 未结 4 1472
情歌与酒
情歌与酒 2021-01-20 05:00

why do we need init() rather than a constructor?

Please answer in reference of Servlet and Applet.
How does t

4条回答
  •  不要未来只要你来
    2021-01-20 05:41

    You need both, but they perform different activities, your constructor executes at the time the object is created through a call to new, but for some type of objects where you don't control their creation, or you would rather execute some code not only after the object is created but is fully intialized, then you need an special method that someone is going to call to signal that the object is ready.

    That is the case specially for objects that are not managed by you, but the server, framework or whoever manages these objects.

    You should see this methods as a commodity provided for you on the top of the code that this object will execute on the constructor

提交回复
热议问题