This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

init method or constructor?

 
Kalai Selvan
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Why init method is required in servlet, can't constructor serve the same purpose?

Thanks in advance.

Kalai Selvan T.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because servlets are created according to textual data in web.xml or a URL, so instances are created like this:



which uses the no-argument constructor. But a servlet needs various initialization parameters to set itself up, so the designers of the servlet APIs added an init() method to which the container could pass the initialization arguments.

Now, in JDK1.2, the java.lang.reflect package was added, and it's now possible to do the equivalent of passing constructor arguments to an object created via newInstance(). But the servlet API was first defined before JDK 1.2 came out, so the init() method was the only way to do things.

Note that applets also have an init() method, for very similar reasons.
 
Kalai Selvan
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest,
Thanks for the brief explanation.....

Kalai Selvan T.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic