This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Are there any side effects of calling servlet's init method from its constructor ?

 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are there any side effects of calling servlet's init method from its constructor. ?
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well some points :

1> we should always use the default constructor created by the compiler .
2> the constructor must be a no argument one.

so since we are not overriding the constructor so there is no any constructor from where you will call the init() method.
and if you do override the default constructor it may lead to potential conflicts as init() may be called twice.

av sinha
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Avi,

we are not overriding the constructor so there is no any constructor from where you will call the init() method.
and if you do override the default constructor it may lead to potential conflicts as init() may be called twice.


As I am taking about overriding the default constructor, init method will definitely be called twice once when the servlet would have been instaciated and the second time by the container. but I could not understand what you mean by potential conflicts ? Could you please explain a bit what type of conflict are you talking about ?
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you talking about calling the init method with no arguments or with a ServletConfig argument?? Generally the code that is placed in the init method is of such nature that they need to be called once. Whether there will be any side effects or not depends on what is in the code. If you've got a System.out.println Statement in it, then there won't be any side effects, but if you are using a limited resource or some lookup or something, then there might be some side effects...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic