• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructor and destroy method

 
Neeraj Vij
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

What will happen in the following cases -

1.) if we define a default constructor.
2.) if we define a constructor with arguments.
3.) if we call the destroy() method from the init(), service, doXXX methods



Thanks,
Neeraj.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neeraj Vij wrote:
1.) if we define a default constructor.

servlet runs without any problem

Neeraj Vij wrote:
2.) if we define a constructor with arguments.


no problem, but you should give default constructor

Neeraj Vij wrote:
3.) if we call the destroy() method from the init(), service, doXXX methods


this question discussed in servlet forum many time. search in javaranch
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to take the time to compose descriptive subjects for your posts; read this for more information.

A title such as "few doubts" is not helpful. What would happen if all posts had such a title?

Please go back and change your post to add a more meaningful subject by clicking the button on your post.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seetharaman venkatasamy wrote:no problem, but you should give default constructor
I'm not sure that I agree.

Constructors, in general, are not very useful in servlets. Most useful initialization work must wait until the init() life-cycle method is called (so that access to the servlet config and the servlet context are available). As such, I cannot recall a single instance where I've defined a servlet constructor.

I don't think that it's particularly useful to define a nullary (no args) constructor when one will be automatically provided by default; that is, unless you have something useful (unlikely) to do in it.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sure that I disagree.

In Java, the default, no-arg constructor is only created for you if you don't explicitly create any other constructors.
So, if you create a constructor that takes arguments (which would be absolutely useless) and don't explicitly create a zero arg constructor, the container will not be able to instanciate your servlet.

The container calls the zero arg constructor.

If you want to pass something to your servlet, use the init method.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Souther wrote:I'm sure that I disagree.
I was being uncharacteristically diplomatic.
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neeraj Vij wrote:3.) if we call the destroy() method from the init(), service, doXXX methods

It's just Java. If you call the destroy() method, the code in that method will be executed.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Neeraj Vij wrote:3.) if we call the destroy() method from the init(), service, doXXX methods

It's just Java. If you call the destroy() method, the code in that method will be executed.
Yeah, this has been covered a bazillion times.

Calling destroy() will not cause the servlet to be destroyed. The container merely calls this method when it is taking the servlet out of service.

It's badly named, in my opinion. Were I setting up the API, it'd be called onDestroy() or something like that.

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Souther wrote:
if you create a constructor that takes arguments (which would be absolutely useless) and don't explicitly create a zero arg constructor, the container will not be able to instanciate your servlet.


thats what i mean "but you should give default constructor"[sorry instead of no-arg i type default ] . i mentioned no problem means you can give argument constructor. but no-arg constructor should be there
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Changes 'should' to 'must' and you'll be right on the money.
 
Neeraj Vij
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have changed the subject. Sorry for the inconvinience caused.

Thanks for the inputs.

I was checking few other sites for the answer on constructor questions. There was no clear answer on it. Some of the forums mentioned that It does not even compile if you provide arg constructor.

what I am able to get it is -

1.) servlet will be unloaded when destroy() will be called by container not our code.
2.) if we give non-arg based constructor. servlet will be instantiated properly and work normally.
3.) if we give arg-based constructor, it will give a runtime exception and not a compile time exception. servlet will not be instantiated.

4.) servlet will be instanitiated if we give both (no/arg) based constructor.

Thanks,
Neeraj.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
#1 is backward.

The container calls the destroy method when the servlet is being taken out of service.
The calling of the destroy method (by the container or from your code), in and of itself, does not destroy the servlet.

See Bear's comment on the naming of the service methods and think of destroy as onDestroy.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Souther wrote:Changes 'should' to 'must' and you'll be right on the money.


Exactly Ben,thanks . Trying to improve my English Communication
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Souther wrote:Changes 'should' to 'must' and you'll be right on the money.


Exactly Ben,thanks . Trying to improve my English Communication
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seems like Home work questions , why don't you try it and post what is happening ???
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!