Why there are no constructors in servlets?

A servlet is just like an applet in the respect that it has an init() method that acts as a constrcutor. 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.


Neeraj Yadav said...

Yes of-course you can have the constructors instead of init().But you shouldn't originally ancient java couldn't invoke constructors with parameters through reflection.

Kuldeep Singh said...

Who said servlet does not has constructor. Every class in java has a default constructor at least and servlet is also is a class so it also has a default constructor. If we talk about constructor with argument then we don't need it because servlet has init() method for initialization purpose.


Interview Questions On Java,Java EE Copyright © 2018. Reads: best tracker