aspose file tools*
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 Soft Skills this week in the Jobs Discussion 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: 255

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: 453

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: 255

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: 9321
    
  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 ?