init() vs. Constructor

Q

Can we use the constructor, instead of init(), to initialize servlet?

✍: FYICENTER.com

A

Yes , of course you can use the constructor instead of init(). There's nothing to stop you. But you shouldn't. The original reason for init() was that ancient versions of Java couldn't dynamically invoke constructors with arguments, so there was no way to give the constructur a ServletConfig. That no longer applies, but servlet containers still will only call your no-arg constructor. So you won't have access to a ServletConfig or ServletContext.

2007-04-03, 6833👍, 0💬