• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructors are for initializing instance variables

 
sai prasanna
Ranch Hand
Posts: 167
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
Generally constructors are for initializing instance variables. but in servlets we are using Life cycle method init(ServletConfig sc) for getting initialization parameters.As servlet is instantiated by container is this the reason we are not writing our own constructor in servlet? if not what is the reason please explain if we pass our own constructor what will be the containers and servlet reaction
thanks in advance
saiprasanna
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13045
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your servlet class has a constructor with parameters, it will never be called by the servlet container.

If your servlet class has a public arg-less constructor, it will be called.

Generally speaking, instance variables one might use in a servlet are related to the servlet environment - for example initialization parameters read from web.xml. Therefore they should be initialized in init as per the API.

Incorrect use of servlet instance variables is the cause of many beginner problems.
Bill
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic