“static” who stole my 5 hours of sleep

“static” members will have only one copy of itself in the class.
So this will also help static members and methods to be easily accessed by outside the class just with the Class without its object.Also, Static methods can have access only to static data.

In my project Network calls (Async) were happening in separate class, but everyone wants to connect to a network, so I have made this network call method “static”. Now I had a handler so that it will give back the network calls result back to respective callers. This receierHandler was used to send back the result, some day I have made it static inside the class to get access inside this method.

The network calls were happening one by one and the receiverHandler as it was a static had a single copy and changed itself for every call, till yesterday my app was requesting kind of only one network call at a time, the static receiver was changing itself and sending the result back, today when I made the tab kind of fragments two network call will be sent in same instance but the result should be sent back to respective fragment correctly, but the last fragment was only receiving all the results, since this network calling framework was written months back I didn’t mind to check this Network framework codes and was pulling my hair all day why the last fragment is only receiving all the other fragments response, then after no sleep of 10 hours of trying various iterations like using activities instead of fragment, the style of creating tab layouts and debugging figured out nothing works and went for a small nap of 2 hours,then I started debugging my App again and checked the hashcode of receiverHandlers and found out that they are pointing out the last fragment once the async call has happened, and opened my Network Framework and found out this culprit “static” and fixed it.

BTW, the one who introduced Android to me (Santhosh) wanted me to explain this for other Java learners, and that’s why this post. 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s