Why we use init() rather Constructor
Asked Answered
C

4

6

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

Please answer in reference of Servlet and Applet.
How does the init() of Applet differ from Servlet?

Criminology answered 21/2, 2012 at 16:36 Comment(2)
possible duplicate of Servlet constructor and init() methodPorphyroid
That are 2 questions. You should ask one question in a question.Porphyroid
C
12

The init() method creates and loads the servlet. But the servlet instance is first created through the constructor (done by Servlet container). We cannot write constructors of a servlet class with arguments in servlet (It will throw Exception). So, They provided a init() method that accepts an ServletConfig object as an argument. ServletConfig object supplies a servlet with information about its initialization (init) parameters. Servlet class cannot declare a constructor with ServletConfig object as a argument and cannot access ServletConfig object.

More info at: http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets6.html

Cider answered 21/2, 2012 at 16:38 Comment(4)
Based on your reference I'd change creates and loads by initializes or prepares.Consolidation
The above is correct, I'd add that splitting object creation and initialization allows the servlet container to re-use the same servlet instance for different requests, reducing time and memory consumption.Consolidation
@madth3: what about applets?Criminology
more or less the same reason: javaworld.com/javaworld/javaqa/1999-10/04-qa-appletconst.htmlCider
P
2

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

Porterporterage answered 21/2, 2012 at 16:41 Comment(0)
I
2

It's a design choice. The servlet spec says that you must provide a no-arg constructor and you can override the init() method to perform initialization tasks. They could have chosen to do otherwise and require servlets to have a single argument constructor (ServletConfig) that optionally throws ServletException. Technically, there is no problem with that as reflection API allows you to invoke any constructor declared in a class.

However having an init() method allows servlet containers to pre-instantiate objects and delay their initialization. It helps separate different stages of the lifecycle.

Personally, I don't think this is a strong design choice. It would have been much more convenient to let the web application provide servlet container with pre-instantiated servlets rather than having to let the container call the constructors of various servlets.

Indocile answered 21/2, 2012 at 16:53 Comment(1)
BTW, as long as your initialization code does not depend on the ServletContext or servlet config, you can put code in the no-arg constructor.Indocile
P
0

init() method is invoked only once and therefore only one instance of the controls will be created.

Permission answered 5/2, 2019 at 19:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.