aspose file tools
The moose likes Servlets and the fly likes init() vs. static {} Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "init() vs. static {}" Watch "init() vs. static {}" New topic

init() vs. static {}

Herbert Wilson

Joined: Nov 18, 2003
Posts: 3
What is the difference between using an init() method as opposed to a static block in a servlet? They both are called once when the servlet is loaded.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 62286

Big difference.
The static block is executed when the class is loaded. The init method is called by the container once important structures like the servlet context and the servlet config have been created and are available for the init() method to peruse.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Don't get me started about those stupid light bulbs.
subject: init() vs. static {}