Why not declare a constructor in servlet?

Technically you can define constructors in servlet. But, the declared constructor cannot access the ServletConfig object or throw a ServletException.

Then why is it not customary to declare a constructor in a servlet? Because the init() method is used to perform servlet initialization. In JDK 1.0 (servlet were written in this version), constructors for dynamically loaded Java classes such as servlets cannot accept arguments. Therefore init() was used to initialize by passing the implemented object of ServletConfig interface and other needed parameters.

Also, Java constructors cannot be declared in interfaces. So, javax.servlet.Servlet interface cannot have a constructor that accepts a ServletConfig parameter. To overcome this, init() method is used for initialization instead of declaring a constructor.

This Servlet tutorial was added on 06/05/2008.

Comments on "Why not declare a constructor in servlet?" Tutorial:

  1. Anonymous says:

    this information is very usefull, thanks u so much . keep posting nice infomation.

  2. Anil R. Chinchawade says:

    Really thanks for providing such an useful information…

  3. Ravi says:

    Nice Information.

  4. accessor says:

    thank u very much for providing use full Q&A’s.

  5. Nagraj says:


  6. swami says:

    thank you for providing the detailed information.

  7. Anonymous says:


  8. Alps says:

    Can you please tell me who calls this init() and when servlet class is instantiated?

  9. Anonymous says:

    The default constructor is call to create the instance after that init() method is call to convert instance to servlet.

  10. Rani says:

    D container calls d init() cuase it controls d servlet lifecycle methods…..:)))))

  11. Anonymous says:

    can u please provide the difference between costructor and init() method in servlet

  12. suyash says:

    Awesome as usual….

  13. piyush kumar says:

    first of all good site with good content management ….

    nicely explained

  14. ravi says:


  15. Naveen says:

    Without calling of constructor(without creating an object of that particular servlet) how we can invoke any mathod ie init() mathod

  16. Hari says:

    Good Explanation

  17. Hari says:

    Servlet container will call the init()

  18. Saroj Kumar says:

    “Without Calling Constructor!!!!!!!!!!!!!!” Where it’s written dude…. Of course constructor (no-arg constructor) is called by container to create instance of Servlet..
    but the job of implementing “ServletConfig” interface, is done after instantiation of the Servlet, so it can’t initialize the servlet at instantiation time. So Container pass this implementation as an argument to the init() method of Servlet to complete its servletness.

  19. Prashant says:

    Thanks for half baked and repetitive information :(

  20. nikhil says:


  21. srikanth goud says:

    right said dude

  22. srikanth goud says:

    nice explanation!! but what exactly do you mean by “servletness”?

  23. srikanth says:

    Why the dynamically loaded classes constructor couldn’t accept arguments???

  24. AVINASH says:

    Great post Sir..

  25. R. Praveen says:

    Can any one give me more clarity on why init()will be used instead of constructor.

  26. Sasi says:

    but its still confusing. Interface doesn’t have constructors. dats right, then how come the servlet have a constructor? When a constructor is used?

Comments are closed for this "Why not declare a constructor in servlet?" tutorial.