Geeks Talk

Prepare for your Next Interview


Welcome to the Geeks Talk forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

Constructor and init()

This is a discussion on Constructor and init() within the Java forums, part of the Software Development category; plz can anybody tell me, What is the difference between constructor and init() method of servlet? Every class has default constructor, and servlets are also user written classes then its ...

Go Back   Geeks Talk > Software Development > Java
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read

Java Java related Issues and Problems

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-18-2007
Junior Member
 
Join Date: Aug 2007
Location: pune
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
pramod.kuber is on a distinguished road
Constructor and init()

plz can anybody tell me,
What is the difference between constructor and init() method of servlet?
Every class has default constructor, and servlets are also user written classes then its constructor gets call and difference between constructor and init()?
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-19-2007
Contributing Member
 
Join Date: Sep 2006
Location: bangalore, india
Posts: 1,016
Thanks: 0
Thanked 91 Times in 72 Posts
psuresh1982 will become famous soon enough
Re: Constructor and init()

The initilizer will run regardless of which constructor get called.

Keep in mind that the initialization happens after the call to the super.constructor and before any code in your constructor. This can make a difference if the super.constructor calls some method that the subclass has overriden which uses gloob. That method will see gloob uninitialized.

More Information..

-------------------
suresh
Reply With Quote
  #3 (permalink)  
Old 09-20-2007
Junior Member
 
Join Date: Sep 2007
Location: New Delhi
Posts: 5
Thanks: 0
Thanked 1 Time in 1 Post
softvijay is on a distinguished road
Re: Constructor and init()

Quote:
Originally Posted by pramod.kuber View Post
plz can anybody tell me,
What is the difference between constructor and init() method of servlet?
Every class has default constructor, and servlets are also user written classes then its constructor gets call and difference between constructor and init()?
Hi,

The init() method is typically used to perform servlet initialization--creating or loading objects that are used by the servlet in the handling of its requests. Why not use a constructor instead? Well, in JDK 1.0 (for which servlets were originally written), constructors for dynamically loaded Java classes (such as servlets) couldn't accept arguments. So, in order to provide a new servlet any information about itself and its environment, a server had to call a servlet's init() method and pass along an object that implements the ServletConfig interface. Also, Java doesn't allow interfaces to declare constructors. This means that the javax.servlet.Servlet interface cannot declare a constructor that accepts a ServletConfig parameter. It has to declare another method, like init(). It's still possible, of course, for you to define constructors for your servlets, but in the constructor you don't have access to the ServletConfig object or the ability to throw a ServletException.

vj
Reply With Quote
  #4 (permalink)  
Old 11-04-2007
Junior Member
 
Join Date: Nov 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
renukaraj is on a distinguished road
Re: Constructor and init()

Quote:
Originally Posted by pramod.kuber View Post
plz can anybody tell me,
What is the difference between constructor and init() method of servlet?
Every class has default constructor, and servlets are also user written classes then its constructor gets call and difference between constructor and init()?
.
JDk1.0 does not support constructor to accept arguments Servlet Config.
Reply With Quote
Reply

  Geeks Talk > Software Development > Java

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
Which constructor will be called in main sandhyatammala OOPS 27 04-10-2009 11:28 PM
constructor emb.mallik C and C++ 1 03-02-2009 04:50 PM
Constructor in place of init() method Lokesh M Java 1 01-31-2008 03:43 PM
Class copy constructor and assignment operator Geek_Guest C and C++ 1 12-21-2007 05:49 AM
why i am getting an error while calling a constructor from another constructor raj_java_j2ee Java 4 07-22-2007 03:39 AM


All times are GMT -4. The time now is 11:40 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2009 GeekInterview.com. All Rights Reserved