Home EducationSubjectsMathematics Subscribe to RSS

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

Answer Question

2 Answers - Sort by: Date | Rating

    Constructor is a very important Concept for OOPS Generally it is used to Initialize the value into variabe. if u 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.
    1 0

    Gauravmcu 

    answered 3 years ago

      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...
      0 0

      Shekarajag 

      answered 6 months ago

        Answer Question - Answers are editable for 5 min.

        If you do not Sign-in or Register your answers will be anonymous,

        your answers may also be checked before going online.

        More

        More

           
           

          Ask a Question via Twitter

          Send a question to @askblurtit and we will publish it online and send you a reply everytime you receive an answer.

          Blurtit Store

          Get T-shirts, hoodies, caps and more at the Blurtit store

          Blurtit International