The moose likes Servlets and the fly likes Init and Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Init and Constructor" Watch "Init and Constructor" New topic

Init and Constructor

Padma NI

Joined: Jun 18, 2003
Posts: 1
what is first invoked when a servlet is fist called? Init or constructor? and why do we need to register servlet with diferent names and have different instances?
srinivas madugula

Joined: Apr 12, 2003
Posts: 6
init is not a static method. so it cannot be called with out creating an instance. to creat an instance you must use constructor.
i dont know why you asked this question.
have a good one
ramu velagaturi

Joined: Jun 20, 2003
Posts: 1
init method is first called
Panagiotis Varlagas
Ranch Hand

Joined: Nov 27, 2000
Posts: 233
init() is an instance method. This means that, in order for the servlet to invoke init() it needs to have an instance of your servlet.
In order to have a instance of your servlet, the servlet container needs to have executed an statement which might look like this:
aServlet = new YourServlet();
I.e. the constructor gets executed first.
Morale of the story: Brush up on your Java fundas!
[ June 20, 2003: Message edited by: Panagiotis Varlagas ]
I agree. Here's the link:
subject: Init and Constructor
It's not a secret anymore!