• Post Reply Bookmark Topic Watch Topic
  • New Topic

Init() vs. Constructor

 
Tanzeem Akhtar
Greenhorn
Posts: 9
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, Well can you tell me that why there is need of "init()" , i mean why can not we initialize the servlet object with the help of constructors?
Thank you.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recall that a servlet is NOT a stand alone application. It has to operate in the context of the servlet container.

Therefore you would either have to always provide a constructor taking the ServletConfig or define an API that uses init() - the convention of a no-args constructor and an init method for objects operating in a container is used in Applets and other Java APIs - predating the servlet API.

So servlets are just following an established pattern.

Bill
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!