How to execute a static method twice

Hi all,
In the attached code, as you can see I call the method at.sendRequestString(), twice from main() method.
The first line in sendRequestString() method is Authenticator.setDefault(new MyAuthenticator());

Authenticator is an abstract class and setDefault is a static method in that class.

When I call sendRequestString() for the second time, Authenticator.setDefault(new MyAuthenticator()); is not executed. ie; Authenticator.setDefault gets executed once and only once.
Does this happen because that Authenticator.setDefault is a static method and it will be executed only once?
If yes, how can I make it work for the second time?
My simple Java code is attached. The username and password given in the code are just sample ones.
Please help with an appropriate solution.
Any help in this regard will be well appreciated with points.
Regards,
Sreejith

Hi, its not Authenticator.setDefault() method do not run twice but some part of code in it does. A static method will keep in memory until being destroyed so the variables in it will keep the same also the whole application cycle if you do not change them. So I think it's the coding of Authenticator.setDefault() affect the behaviour. You should look at the source code or any documentation to look for the reason.

Quincy

0

Sreejith22Author Commented: 2008-11-12

Hi quincydude,
Thanks.
Here is the Authenticator.setDefault() method.

Literally, I am seeking a solution, so that I can get out of this mess.

I think it's System.getSecurityManager() that plays the trick.
If it get the SecurityManager, the whole piece of code do not execute.
You should find a method to reset it after your sendRequestString() finishes.

As i mentioned earlier, you should try to reset the important elements (IOW the username and password) of the Authenticator, not the authenticator itself. You can use normal mutator methods to do this and then use the accessor methods to perform the significant method of the Authenticator below. If this doesn't work (you don't have control of when the Authenticator gets called, then use an API such as Apache HttpClient, which will give you more control over the authentication process

Below given is the final implementation, by correctly taking into consideration, each of your suggestions and comments. Still the result is the same.

Though, password authentication works correctly for both the combinations, it still does not have any effect in general and the output obtained for second combination is the same as that of the first.
So, this implies that problem is concerned with setDefault rt?