File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Init() Vs Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Init() Vs Constructor" Watch "Init() Vs Constructor" New topic

Init() Vs Constructor

Niranjan Prasad
Ranch Hand

Joined: Aug 13, 2005
Posts: 62
Constructor is for initialization and so y they introduced init()?
Badri Sarma
Ranch Hand

Joined: Apr 01, 2003
Posts: 72
init method is the life cycle method of servlet and constructor by default it exist if it is a java class.
If you look at the API for the following method init(ServletConfig config) it says
"Called by the servlet container to indicate to a servlet that the servlet is being placed into service. "

Through constructor container cannot place servlet for the service.

init is more specific to the container.

Thanks<br />Badri
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

This gets asked quite often here.

You can either look at our entry in the FAQ:

Or search this forum with keywords "constructor" and "init" to find the question asked and answered many times.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
I agree. Here's the link:
subject: Init() Vs Constructor
It's not a secret anymore!