This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Are there any side effects of calling servlet's init method from its constructor ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Are there any side effects of calling servlet Watch "Are there any side effects of calling servlet New topic
Author

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

Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 254

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


Regards
Salil Verma
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 452

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


SCJP 5.0 SCWCD 5.0
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 254

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

Joined: Aug 03, 2008
Posts: 9293
    
  17

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


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Are there any side effects of calling servlet's init method from its constructor ?