What Is The Difference Between A Constructor And The Init Method?

2

2 Answers

Anonymous Profile
Anonymous answered
Constructor is a Java basic feature of Java.and Init method is designed in such a way that it acts as a constructor but it just a function which returns nothing and called by container not by the user.So constructor gets executed at any cost but init method should be called by the container after executing constructor normally.So instead writing the constructor it is better to write code in init() method which is also acts as constructor.

But in servlet when the Servlet instantiated by the container which means constructor called first then init method after calling the init method servlet is ready to accept the requests and those requests are processed thru service method.Once destroy method is called by the container the servlet is not available for serving the clients and for some web servers when a new user comes to the servlet it calls the init() method not the constructor...

Hope you understand the differences...
Gaurav Kumar Profile
Gaurav Kumar answered
Constructor is a very important Concept for OOPS Generally it is used to Initialize the value into variabe. If you don't  create constructor  then it will be automatically created when object is created .

But init method is used for initialization, which  means It initializes the local as well as global variable on server during using of post /get method with diff protocol used,The init method is generally used for Servlet as well as with JSP.

Answer Question

Anonymous