Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Why is init() needed? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Why is init() needed?" Watch "Why is init() needed?" New topic
Author

Why is init() needed?

Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
When there is a constructor available for every servlet, what is the needed for a separate method init()?
Mathias Nilsson
Ranch Hand

Joined: Oct 13, 2003
Posts: 107
A servlet is just like an applet in the respect that it has an init() method that acts as a constructor. Since the servlet environment takes care of instantiating the servlet, an explicit constructor is not needed. Any initialization code you need to run should be placed in the init() method since it gets called when the servlet is first loaded by the servlet container.
There is no reason to include a non-default constructor within s aervlet, because it would never get called. Do all your initializing in init().
// MAthias


SCJP2 , MCP( 70-229 ) , Preparing For SCWDC
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: Why is init() needed?
 
It's not a secret anymore!